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");
}

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