Aamir Mansoor Aamir Mansoor - 1 year ago 633
Objective-C Question

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

didn't help since
doesn't have a

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

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

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

Any ideas on how to go about solving this?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download