I have an NSButton in a mac application whose colour I'd like to change programatically but nothing I tried seems to work. I tried to create an output on the NSButtonCell and set the background color there but that didn't work either. Any code snippets would be helpful.
m file [[myButton cell] setBackgroundColor:[NSColor redColor]]; Note from the setBackgroundColor documentation: "The background color is used only when drawing borderless buttons." If this won't do it for you then you'll need to override NSButton and implement the drawing yourself.
First let us see using storyboard, Open Main. storyboard and add one view to the View Controller. On the right pane you can see the property, and from there update the background color to color you want your view to be as show below.
At the top select the attributes inspector. Under the section "View" there should be a spot that says "Background". click that and choose your colour.
Assuming everything is hooked up in IB for your borderless button.
// *.h file IBOutlet NSButton* myButton; // *.m file [[myButton cell] setBackgroundColor:[NSColor redColor]];
Note from the setBackgroundColor documentation:
"The background color is used only when drawing borderless buttons."
If this won't do it for you then you'll need to override NSButton and implement the drawing yourself.
Good Luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With