【问题标题】:MacOS Playground: How can change the background color?MacOS Playground:如何更改背景颜色?
【发布时间】:2019-04-09 20:29:28
【问题描述】:

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

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()

但视图不会改变颜色:

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

非常感谢您的帮助

【问题讨论】:

  • 是“Hello world!”在笔尖?是self.view.layernil
  • 操场上的笔尖在哪里?
  • 菜单视图->导航器->显示项目导航器。 xib 位于 Resources 文件夹中。
  • @Willeke,谢谢。我真的很感激

标签: swift xcode macos swift-playground


【解决方案1】:

您只需将视图的 WantLayer 属性设置为 true

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

【讨论】:

猜你喜欢
  • 2020-11-28
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2018-02-13
  • 2012-01-02
  • 2022-01-21
  • 2016-05-31
相关资源
最近更新 更多