文章目录
一、关于黑暗模式
今年的 WWDC 已公布了 macOS 10.15, mojave 已是去年的系统版本,最近才更多的关注黑暗模式的适配。好处是,文章大家都写了,所以下面的内容均是摘抄总结,先贴上地址吧:
-
支持你的黑暗模式
https://developer.apple.com/documentation/appkit/supporting_dark_mode_in_your_interface?language=objc -
How to enable “real dark mode” on OS X / macOS
https://medium.com/@guilhermerambo/how-to-enable-real-dark-mode-on-os-x-macos-14966f9f7d24 -
Dark Side of the Mac: Updating Your App
https://mackuba.eu/2018/07/10/dark-side-mac-2/
https://stackoverflow.com/questions/37359825/how-do-i-implement-night-mode-in-mac-cocoa-application
http://www.sohu.com/a/256885010_115785
- TomatosX: 判断OS X是否为深灰色菜单栏(status bar)和Dock
https://www.jianshu.com/p/91400658a63b
二、命令行 启用/关闭 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
三、检测黑暗模式
1、命令行 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
2、代码 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");
}
五、其它
1、非黑暗模式下的黑暗侧边栏
https://github.com/rensbreur/DarkSidebar/blob/master/screenshot.png