NSColor systemColor not changing when dark/light mode switched

前端 未结 1 941
感情败类
感情败类 2021-01-20 08:35

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 i

相关标签:
1条回答
  • 2021-01-20 09:11

    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]];
        }
    }
    
    0 讨论(0)
提交回复
热议问题