在ScreenSaverView,Swift中从xib加载视图

时间:2018-03-18 08:00:29

标签: swift xcode macos nsview screensaver

我在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一个!

0 个答案:

没有答案