We have a window we want to display, and in 10.11, it displays as we expect.
we set all the properties of the views within the window in -windowDidLoad
, and when the window is displayed, these buttons have the right colors:
- (void)windowDidLoad
{
[super windowDidLoad];
LightTheme *lightTheme = [[LightTheme alloc] init];
_cancelButton.backgroundColor = lightTheme.controlColor;
_stopButton.backgroundColor = lightTheme.controlColor;
_cancelButton.textColor = lightTheme.textColor;
_stopButton.textColor = lightTheme.textColor;
}
But, with this same code in 10.10, the colors are set to the default values for this button subclass
What's interesting is that if you interact with the buttons, they immediately redraw and have the right white background
However, if we move the code to -awakeFromNib, it looks good on both OSs, as soon as the window is displayed
- (void)awakeFromNib
{
[super awakeFromNib];
LightTheme *lightTheme = [[LightTheme alloc] init];
_cancelButton.backgroundColor = lightTheme.controlColor;
_stopButton.backgroundColor = lightTheme.controlColor;
_cancelButton.textColor = lightTheme.textColor;
_stopButton.textColor = lightTheme.textColor;
}
Did they change when the window is displayed from 10.10->10.11? Or are we missing something else?
It seems like it used to be:
-awakeFromNib -> Display Window
Now it looks like:
-windowDidLoad -> Display Window
EDIT: Here is the how the backgroundColor
property is used:
First, get a color depending on button state
-(NSColor*)effectiveBackgroundColor
{
NSColor *result = ([self isOn] && self.backgroundAlternateColor) ? self.backgroundAlternateColor : self.backgroundColor;
if ( !self.enabled && result ) // if disabled, dim the background
result = [result colorWithAlphaComponent:self.disabledOpacity];
return result;
}
Then in the -drawRect
, call this method:
-(void)drawBackground
{
NSSize inset = [self buttonInset];
NSRect bodyRect = self.bounds;
bodyRect = NSInsetRect(bodyRect, inset.width, inset.height);
NSBezierPath* buttonPath = [NSBezierPath bezierPathWithRoundedRect:bodyRect xRadius:self.cornerRadius yRadius:self.cornerRadius];
NSColor* effectiveBackgroundColor = [self effectiveBackgroundColor];
if (effectiveBackgroundColor)
{
[NSGraphicsContext saveGraphicsState];
[[self effectiveBackgroundShadow] set];
[effectiveBackgroundColor setFill];
[buttonPath fill];
[NSGraphicsContext restoreGraphicsState];
if( [[self cell] isHighlighted] )
{
[[[NSColor blackColor] colorWithAlphaComponent:self.highlightOpacity] setFill];
[buttonPath fill];
}
}
}