我正在尝试更改主视图的背景颜色:
import AppKit
import PlaygroundSupport
class ViewController : NSViewController {
override func loadView() {
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
Bundle.main.loadNibNamed(
nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { $0 is NSView }
self.view = views[0] as! NSView
self.view.layer?.backgroundColor = NSColor.white.cgColor
}
}
PlaygroundPage.current.liveView = ViewController()
但是 View 并没有改变颜色:
还有“Hello world”标签是从哪里来的?
非常感谢你的帮助
最佳答案
您只需要将 View 的 wantsLayer 属性设置为 true
let view = views[0] as! NSView
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.blue.cgColor
关于swift - MacOS Playground : How can change the background color?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55600853/