Joe Habadas
Joe Habadas

Reputation: 628

Objective-C, NSPopMenuButton & NSMenu Color

I have an NSPopMenuButton which is connected to an NSMenu in the standard way. I tried sub-classing both in an attempt to change the background color of the menu itself. I'm clearly not doing something correctly, so any advice would be helpful.

Tried (NSPopUpButton) customPopUpButton.m:

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
    [[NSColor grayColor] set];
    NSRectFill(dirtyRect);

}

Which gave me: not good I'd actually rather it be like: better

I tried creating another class to override NSPopUpButtonCell as suggested by another answer, but I must not know how to implement it correctly as it seems to have no effect other than what the code above does.

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

    [[NSColor grayColor] set];
    NSRectFill(cellFrame);

    [super drawInteriorWithFrame:cellFrame inView:controlView];

}

Something to note is that my deployment target is macOS 10.11 if that makes any difference.

Upvotes: 1

Views: 590

Answers (1)

anonymous
anonymous

Reputation: 11

Your customized NSPopUpButton drawing is filling the entire drawing area with color. Default title drawing is missing (under the filled color).

Try customizing NSPopUpButtonCell drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView.

Upvotes: 1

Related Questions