在Swift 2.0中更改NSView的背景颜色 [英] Changing background color of NSView in Swift 2.0

查看:355
本文介绍了在Swift 2.0中更改NSView的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改NSView的背景色,并尝试了

I am trying to change the background color of NSView and have tried one of the solution outlined in this answer. However, it's not working for me. What am I missing here? Should I use viewWillAppear() here or it only meant for 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 beta4.

P.S - I'm new to OS X/iOS programming and trying to figure my way out through documentation and existing solutions. Using 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 {

        }
    }
}

这篇关于在Swift 2.0中更改NSView的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆