Swift 子类化NSWindow的正确方法
我正在尝试添加这样一个窗口:Swift 子类化NSWindow的正确方法,swift,macos,cocoa,Swift,Macos,Cocoa,我正在尝试添加这样一个窗口: customWindow = NSWindow.init(contentRect: rect, styleMask: styleMask, backing: NSBackingStoreType.Buffered, defer: false) customWindow?.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.3) customWindo
customWindow = NSWindow.init(contentRect: rect, styleMask: styleMask, backing: NSBackingStoreType.Buffered, defer: false)
customWindow?.backgroundColor = NSColor(calibratedHue: 0, saturation: 1.0, brightness: 0, alpha: 0.3)
customWindow?.ignoresMouseEvents = true
customWindow?.level = Int(CGWindowLevelForKey(.MaximumWindowLevelKey))
customWindow?.makeKeyAndOrderFront(customWindow)
它应该显示出来,但我想对它进行子类化
class Screen: NSWindow {
init() {
let screen = NSScreen.mainScreen()
let frame = NSMakeRect(0, 0, (screen?.frame.size.width)!, (screen?.frame.size.height)!)
let styleMask = NSBorderlessWindowMask
let rect = NSWindow.contentRectForFrameRect(frame, styleMask: styleMask)
super.init(contentRect: rect, styleMask: styleMask, backing: .Buffered, defer: false)
self.styleMask = styleMask
self.backingType = NSBackingStoreType.Buffered
self.setFrame(rect, display: true)
self.backgroundColor = NSColor(calibratedHue: 0.5, saturation: 1.0, brightness: 0, alpha: 0.6)
self.ignoresMouseEvents = true
self.level = Int(CGWindowLevelForKey(.MaximumWindowLevelKey))
}
}
我试着这样添加它:
let win = Screen()
win.makeKeyAndOrderFront(win)
我不熟悉这一点,花了半天的时间研究它是如何完成的,没有运气。我不是一个敏捷的专家,但在Objective-C中,当你调用
超级init
,它会返回实例化的对象,你将其分配给self
。因此,这行代码看起来像是self=[super init]
(根本不确定这是否适用,只是注意到我观察到的一些东西)对于初学者来说,屏幕init()
行应该是覆盖init()
…你的问题是什么?@rocky窗口不显示。