gpt4 book ai didi

swift - MacOS Playground : How can change the background color?

转载 作者:行者123 更新时间:2023-11-28 11:35:12 32 4
gpt4 key购买 nike

我正在尝试更改主视图的背景颜色:

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 并没有改变颜色:

enter image description here

还有“Hello world”标签是从哪里来的?

非常感谢你的帮助

最佳答案

您只需要将 View 的 wantsLayer 属性设置为 true

let view = views[0] as! NSView
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.blue.cgColor

enter image description here

关于swift - MacOS Playground : How can change the background color?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55600853/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com