我在view
中有一个自定义ScreenView.xib
,我想加载ScreenSaverView
。我的ScreenSaverView
子类如下:
class SwiftScreensaverView: ScreenSaverView {
override init?(frame: NSRect, isPreview: Bool) {
super.init(frame: frame, isPreview: isPreview)
self.wantsLayer = true
self.addSubview(self.loadNib())
}
func loadNib() -> NSView {
var topLevelObjects : NSArray?
if Bundle.main.loadNibNamed(NSNib.Name(rawValue: "ScreenView"), owner: self, topLevelObjects: &topLevelObjects) {
let newView = (topLevelObjects!.first(where: { $0 is NSView }) as? NSView)!
newView.frame = self.frame
newView.wantsLayer = true
newView.layer?.backgroundColor = NSColor.red.cgColor
return newView
}
let oldView = NSView.init(frame: self.frame)
oldView.wantsLayer = true
oldView.layer?.backgroundColor = NSColor.green.cgColor
return oldView
}
}
但是,预览中的屏幕保护程序会以green
颜色显示视图,而不是red
一个!