Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background color of NSButton

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.

like image 621
aleemb Avatar asked Jun 19 '09 11:06

aleemb


People also ask

How do I change the background color in NSButton?

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.

How do you change the background color on a storyboard?

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.

How do I change the background color in Xcode?

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.


1 Answers

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.

like image 80
Mark Avatar answered Sep 27 '22 23:09

Mark