How can dark mode be detected on macOS 10.14?

前端 未结 6 1337
天涯浪人
天涯浪人 2020-12-13 20:51

In macOS 10.14 users can choose to adopt a system-wide light or dark appearance and I need to adjust some colours manually depend of the current mode.

6条回答
  •  清歌不尽
    2020-12-13 21:18

    I have used the current appearance checking if the system is 10.14

    + (BOOL)isDarkMode {
        NSAppearance *appearance = NSAppearance.currentAppearance;
        if (@available(*, macOS 10.14)) {
            return appearance.name == NSAppearanceNameDarkAqua;
        }
    
        return NO;
    }
    

    And to detect the change of mode in a view the methods are:

    - (void)updateLayer;
    - (void)drawRect:(NSRect)dirtyRect;
    - (void)layout;
    - (void)updateConstraints;
    

    And to detect the change of mode in a view controller the methods are:

    - (void)updateViewConstraints;
    - (void)viewWillLayout;
    - (void)viewDidLayout;
    

    Using notification:

    // Monitor menu/dock theme changes...
    [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
    
    -(void)themeChanged:(NSNotification *) notification {
        NSLog (@"%@", notification);
    }
    

    For more information Dark Mode Documentation

提交回复
热议问题