NSButton backgroundColor

moin community,

I would like to do something like that for macOS:

button.backgroundColor = NSColor.red

have now searched the internet for 2 hours and found nothing :disappointed_relieved:
it has to go somehow or?

Kind regards Carl

This question is more about AppKit than about Swift. I’ve included a basic answer below but, be warned, I mainly focus on networking so my knowledge of UI frameworks is rather limited. If you have follow-up questions I encourage you to post them to the App Frameworks > Cocoa area of DevForums.


Unlike UIKit, AppKit does not have a backgroundColor property on every view. However, some views do have specific support for background colours. In the case of NSButton, this isn’t in the NSButton itself but in the nested NSCell. So, you can do this as follows:

(button.cell! as! NSButtonCell).backgroundColor = .red

IMPORTANT This only works if the button has no border (that is, you disable Bordered in the Attributes inspector in Interface Builder).

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple