Wil*_*haw 2 macos xcode cocoa swift
试图以编程方式添加一个简单的 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)
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
import Cocoa
let frame = CGRect(origin: .zero, size: CGSize(width: 100, height: 100))
let view = NSView(frame: frame)
view.wantsLayer = true
view.layer?.backgroundColor = NSColor.black.cgColor
self.view.addSubView(view)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
900 次 |
| 最近记录: |