views:

2140

answers:

3

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.

+2  A: 

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.

Mark Thalman
+1  A: 

The standard Aqua (pill-shaped) buttons are drawn by the system. They don't have a background color as such. In fact, they are composed of images that Cocoa stitches together to make a coherent button image. So Cocoa can't recolor the images to your liking. Only the default button (the one with Return set as its key equivalent) will have a blue pulsing background.

What it sounds like you want to do will involve subclassing NSButtonCell and doing your own drawing. If you wanted to recolor the button images to get the effect you want, you can use the excellent Theme Park utility to extract copies of Apple's button images and use those. I'll admit to having done this myself in order to "steal" certain interface elements that aren't otherwise accessible, such as the scrollers from iTunes.

Alex
That explains why I can't just modify the background property. Theme Park is a real gem!
aleemb
A: 

UIElement(Element is of anything Like Button,view....)

UIElement *varname;

[varname setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.6 alpha:1.0]];

eg: UIButton *pButton;

[pButton setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.6 alpha:1.0]];

Lova Rajesh
That won't help for an NSButton.
Peter Hosey