1

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.

Joe Habadas
  • 628
  • 8
  • 21
  • Does this answer your question? [How to change the background color of an NSPopupButton?](https://stackoverflow.com/questions/13222205/how-to-change-the-background-color-of-an-nspopupbutton) – Willeke Mar 29 '20 at 11:41
  • See [How to set color of NSPopupButton Menu Item](https://stackoverflow.com/questions/4731982/how-to-set-color-of-nspopupbutton-menu-item) – Willeke Mar 29 '20 at 11:42
  • @Willeke, I tried that actually but I don't know how to properly implement the suggestion in the answer. I added a new sub-class to `NSPopUpButtonCell` and put the method in, but it seems to do nothing that the original code I posted does. – Joe Habadas Mar 29 '20 at 16:28

1 Answers1

1

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.

Willeke
  • 14,578
  • 4
  • 19
  • 47
anonymous
  • 11
  • 2
  • Can you please show an example of how to implement this? I tried and nothing happens other than what already is taking place. – Joe Habadas Mar 29 '20 at 16:29
  • 1
    @Joe Habadas,When you used customized nspopupbuttoncell,did you remove customization done for nspopupbutton? – anonymous Mar 30 '20 at 08:11
  • I finally got the NSPopUpButtonCell to show the color correctly with the text after removing the customization in NSPopUpButton, but the NSPopUpButtonCell did not colorize the NSMenu items, so I guess that is something else I need to subclass? In the meantime I selected "Dark Aqua" for the View's Appearance, which worked. I would like to do that programmatically though if possible. – Joe Habadas Apr 03 '20 at 05:29
  • @JoeHabadas, You can set the appearance programmatically; [someView setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]]; – anonymous Apr 03 '20 at 12:29