I'm trying to change the colour of an image with the switching of dark/light mode in an NSViewController.
I'm using this code for changing the colour of the image:
- (NSImage *)image:(NSImage *)image withColour:(NSColor *)colour
{
NSImage *img = image.copy;
[img lockFocus];
[colour set];
NSRect imageRect = NSMakeRect(0, 0, img.size.width, img.size.height);
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[img unlockFocus];
return img;
}
I've tried calling this method from viewWillLayout
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
but it seems the system color always returns the same RGB values.
I've also tried listening for the notification AppleInterfaceThemeChangedNotification but even in here it seems the RGB values stay the same 1.000000 0.231373 0.188235.
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:@"AppleInterfaceThemeChangedNotification"
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"AppleInterfaceThemeChangedNotification");
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];
NSColor *testColor = [[NSColor systemBlueColor] colorUsingColorSpace:colorSpace];
CGFloat red = [testColor redComponent];
CGFloat green = [testColor greenComponent];
CGFloat blue = [testColor blueComponent];
NSLog(@"%f %f %f", red, green, blue);
}];
I have the working fine in an NSButtonCell sublass and overriding layout but can't get it working in an NSViewController
First, check the documentation section "Update Custom Views Using Specific Methods" here. It says:
However, there are no
NSViewControllermethods listed in that table.Since the view's appearance can be independent of the current or "system" appearance, the best way to react to appearance changes in your view controller is to either KVO the view's
effectiveAppearanceproperty, or to do something in[NSView viewDidChangeEffectiveAppearance].NSAppearancehas acurrentAppearanceproperty which is independent of the system appearance, and updated by Cocoa in the methods listed above. Everywhere else, you will need to check that is correct yourself. The idiomatic way is, again, via the view'seffectiveAppearance:So, in your case, the following works well for me: