尝试以编程方式添加简单的NSView,但它未出现。
** 答案 **:在.backgroundColor = .black之前执行.wantsLayer = true
我将颜色设置为黑色,因此我期望在窗口中有一个黑色的正方形。
我正在使用Xcode 10.2 Swift 5。
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let newView = NSView(frame: NSRect(x: 10,y: 10, width: 100,height: 100))
newView.layer?.backgroundColor = .black
newView.wantsLayer = true
self.view.addSubview(newView)
}
}