我最近开始编写 Swift 代码。因此,我正在开发一个 macOS 应用程序,以更好地适应 Swift。
我希望在用户按下按钮时创建 NSTextField。我得到了下面的代码,但 NSTextField 不显示。在研究时,我发现它必须以某种方式添加到 View 中,但不是如何完成的。
@IBAction func buttonPressed(_ sender: Any) {
let label = NSTextField()
label.stringValue = ("Hello World")
}
如何让 NSTextField 显示?任何帮助将不胜感激。
最佳答案
只需将标签转换为图层支持的 View 即可。然后将其添加到可见 View 中:
let rect = NSMakeRect(20, 20, 80, 44)
let label = NSTextField(frame: rect)
label.wantsLayer = true
label.layer?.backgroundColor = NSColor.greenColor().CGColor
window?.contentView?.addSubview(label)
关于swift - NSTextField 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202124/