Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of an NSPopupButton?

I am trying to tackle a problem which sounds pretty simple: changing the background color of an NSPopupButton.

Interface Builder only allows changing the style to a pre-defined one and doesn't allow changing the background color. Also, setting up an IBOutlet didn't help since NSPopupButton doesn't have a setBackgroundColor method.

I also tried subclassing NSPopupButton to override the drawRect method. Here's what I have tried:

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor redColor] setFill];
    NSRectFill(dirtyRect);
}

This draws a red rectangle over the NSPopupButton rather than setting it as a background color.

Any ideas on how to go about solving this?

like image 784
Aamir Avatar asked Nov 04 '12 19:11

Aamir


1 Answers

You should create a subclass of NSPopUpButtonCell, then override

- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView

NSPopupButtonCell is a subclass of NSButtonCell which defines several methods for drawing individual cell components, eg bezel, title, image.

You can then expand the NSPopupButton and change its cell subclass to your new subclass and it should use your drawing methods.

Cocoa primarily uses NSCell to handle drawing, unlike iOS

like image 178
DanBrooker Avatar answered Oct 15 '22 02:10

DanBrooker