xcode - 在 Swift 2.0 中更改 NSView 的背景颜色

标签 xcode macos swift

我正在尝试更改 NSView 的背景颜色,并尝试了 this answer 中概述的解决方案之一。 .但是,它对我不起作用。我在这里错过了什么?我应该在这里使用 viewWillAppear() 还是仅适用于 iOS?

相关代码:

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.wantsLayer = true

    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    override func awakeFromNib() {
        if self.view.layer != nil {
            let color : CGColorRef = CGColorCreateGenericRGB(1.0, 0, 0, 1.0)
            self.view.layer?.backgroundColor = color
        }

    }
}

P.S - 我是 OS X/iOS 编程的新手,并试图通过文档和现有解决方案找到出路。使用 Xcode 7 beta 4。

最佳答案

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor(red: 1, green: 0, blue: 0, alpha: 1).CGColor

    }

    override var representedObject: AnyObject? {
        didSet {

        }
    }
}

关于xcode - 在 Swift 2.0 中更改 NSView 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32199161/

相关文章:

cocoa - Xcode调试: View value of NSNumber?

ios - 快速传递方 block

iphone - 无法将 UITableView 的样式更改为在 Interface Builder 中分组

iphone - 使用不同的分发配置文件 - 我的应用程序仍然可以工作吗?

ios - 更新到 Swift 2.0 后的自动布局问题

objective-c - 什么可能导致无法在一台计算机上启动 OS X 应用程序的两个副本?

objective-c - 从存储在单例中的数组中删除对象

macos - 使用 Swift 将邮件从 Mail 拖到 Dock

ios - 重新创建 iOS 1 3' share sheet modal in swift (not the share sheet itself, but the way it' s 呈现)

ios - 按顺序重复运行两个 SKActions?