macosnscolormacos-darkmode

NSColor systemColor not changing when dark/light mode switched


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


Solution

  • First, check the documentation section "Update Custom Views Using Specific Methods" here. It says:

    When the user changes the system appearance, the system automatically asks each window and view to redraw itself. During this process, the system calls several well-known methods for both macOS and iOS, listed in the following table, to update your content. The system updates the trait environment before calling these methods, so if you make all of your appearance-sensitive changes in them, your app updates itself correctly.

    However, there are no NSViewController methods 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 effectiveAppearance property, or to do something in [NSView viewDidChangeEffectiveAppearance].

    - (void)viewDidLoad 
    {
        [self addObserver:self forKeyPath:@"view.effectiveAppearance" options:0 context:nil];
    }
    
    // ...
    
    - (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
    {
        if ([keyPath isEqualToString:@"view.effectiveAppearance"])
        {
    // ...
    
    

    NSAppearance has a currentAppearance property 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's effectiveAppearance:

    [NSAppearance setCurrentAppearance:someView.effectiveAppearance];
    

    So, in your case, the following works well for me:

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
    
        [self addObserver:self forKeyPath:@"view.effectiveAppearance" options:0 context:nil];
    }
    
    -(void)viewDidLayout
    {
        self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([keyPath isEqualToString:@"view.effectiveAppearance"])
        {
                    [NSAppearance setCurrentAppearance:self.view.effectiveAppearance];
    
                    self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
        }
    }