简体   繁体   English

在OSX中更改按钮的背景色

[英]Change Background Color of button in OSX

I want to change the background color of a button in a dialog to red, if a specific option is enabled. 如果启用了特定选项,我想将对话框中按钮的背景颜色更改为红色。 I tried 我试过了

run.backgroundColor = NSColor.red

but nothing happens. 但什么也没发生。 (run is the name of the button) There are similar questions, but no answers. (运行是按钮的名称)有类似的问题,但没有答案。 And these questions are years old. 这些问题已有多年历史了。 I hope there is a simple solution, bc. 我希望有一个简单的解决方案,BC。 this seems not an unusual approach. 这似乎不是一个不寻常的方法。 Again, it belongs to OSX (and SWIFT 3). 同样,它属于OSX(和SWIFT 3)。

NSButton doesn't have a backgroundColor property, but it's CALayer does. NSButton没有backgroundColor属性,但是CALayer有。 So, to get the same effect you could do the following: 因此,要获得相同的效果,您可以执行以下操作:

myButton.wantsLayer = true
myButton.layer?.backgroundColor = NSColor.red.cgColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM