macOS 开发 - Dark Mode 黑暗模式(10.14 mojave)


今年的 WWDC 已公布了 macOS 10.15, mojave 已是去年的系统版本,最近才更多的关注黑暗模式的适配。好处是,文章大家都写了,所以下面的内容均是摘抄总结,先贴上地址吧:

https://stackoverflow.com/questions/37359825/how-do-i-implement-night-mode-in-mac-cocoa-application

http://www.sohu.com/a/256885010_115785


命令行 启用/关闭 dark模式

defaults write -g NSRequiresAquaSystemAppearance -bool No

关闭某应用的黑暗模式:

defaults write com.google.Chrome NSRequiresAquaSystemAppearance -bool YES  //关闭深色模式

defaults write com.google.Chrome NSRequiresAquaSystemAppearance -bool NO //启用
defaults write -g NSWindowDarkChocolate -bool TRUE  

defaults write -g NSWindowDarkChocolate -bool FALSE

检测黑暗模式

命令行 defaults read -g AppleInterfaceStyle

黑暗模式下返回 Dark

$ defaults read -g AppleInterfaceStyle
Dark

非黑暗模式

$ defaults read -g AppleInterfaceStyle
2019-06-12 15:04:55.644 defaults[9672:767112] 
The domain/default pair of (kCFPreferencesAnyApplication, AppleInterfaceStyle) does not exist


代码 AppleInterfaceStyle


NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle”];

NSLog(@"osxMode : %@",osxMode);  //黑暗模式打印:Dark  非黑暗模式: (null)

应用不采用黑暗模式

很多应用不需要使用黑色模式,但在黑色模式下,窗口边框可能是黑色,或者有其他字体渲染问题。那么需要以下设置:

在 info.plist 中添加键值对:
NSRequiresAquaSystemAppearance 设置为 YES , 即不采用黑暗模式。


黑色模式的切换通知

用户切换黑色模式,系统会发出通知。有的小伙伴可以根据这个通知来修改应用。

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
-(void)darkModeChanged:(NSNotification *)notif {
    NSLog(@"Dark mode changed");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章