1

I am coming from iOS/Android programming background and working on my first MAC application. I am using an NSComboBox to list some item that user can choose but having some trouble setting the background color of the drop down menu.

What I have currently?

This is what I have now. You can see the 2 options in drop down coming in a white background (forget the blue, it shows the item is selected). I want to change the white to some other color

enter image description here

What I have done?

I did some searching and come across this thread. The answer suggests subclassing NSComboBoxCell and overriding - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView method. I tried it out.. This is my code

@interface CustomComboBoxCell : NSComboBoxCell
@end

@implementation CustomComboBoxCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
    [super drawWithFrame:cellFrame inView:controlView];

    //I want red color to the dropdown menu, I filled with yellow color
    NSRect bounds = NSMakeRect(cellFrame.origin.x, cellFrame.origin.y,
                     cellFrame.size.width, cellFrame.size.height);
    [[NSColor redColor] setFill];
    NSRectFill(bounds);
}
@end

and this is the result

enter image description here

Not what I wanted. :(

Question

Simple, how can I change the background color of drop down menu of NSComboBox?

Anyone?

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • Well, I shipped the app without fixing this. Such a basic thing!! – Krishnabhadra Jul 22 '13 at 09:52
  • 1
    I know this is a rather old question, but I just encountered this exact issue as well today. After some reverse engineering, I found that the NSComboBoxCell has an instance variable called _tableView, which is actually the drop down menu itself. So if you want a quick, dirty and non-reliable solution that would break in the future, you can subclass combobox cell and change the tableview background color to your desired color. That said, this is quick and dirty and is not future-proof. BTW, this is with the 10.8 SDK. – Enzo Aug 15 '14 at 20:08

2 Answers2

0

Well, I used a NSButton with menu, so I could use NSMenuItems with custom views. That's not really what you need, But you can make a combo-box replacement with NSTextField, NSButton and NSMenu. Maybe that would be easier.

Divyu
  • 1,325
  • 9
  • 21
silvansky
  • 2,426
  • 2
  • 19
  • 20
  • I have thought about it..And it is what I am going to do if I didn't find any other method. But I can't believe there is no straightforward way to change the background color in API, very strange.. – Krishnabhadra Jun 13 '13 at 10:00
0

I played around a bit. Would this work for you:

enter image description here

If you want to change each title in the cell, it may be possible to set the tag (also in IB).

ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • From what I understand from the picture - you ticked `drawsBackground`, Set backgroundColor to Green, and thats it, right? Nope I tried that and it didn't work for me. – Krishnabhadra Jun 20 '13 at 03:20