Reputation: 5440
I have a custom subclass of NSSearchField
that I would like to set the background color of.
@interface CustomNSSearchField : NSSearchField
@end
So far, I have tried:
Attempt #1
@implementation CustomNSSearchField
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
[self setDrawsBackground:YES];
[self setBackgroundColor:[NSColor redColor]];
}
which resulted in no visual changes at all:
I then followed the suggestions here and also tried:
Attempt #2
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
[[NSColor redColor] setFill];
NSRectFill(rect);
}
Which results in this:
How do I set the background color inside the bounds and behind the text of the search field?
Upvotes: 2
Views: 1704
Reputation: 6862
You have to redraw the entire thing. There is no property, to specifically change the background-color of the NSSearchField. Check out this example:
Edit:
Also what's worth to point out. You should never override the controls drawRect method. You should rather make a subclass of NSSearchFieldCell.
Upvotes: 1