我的 Mac 应用需要根据浅色或深色模式更改行为。
在 macOS Catalina 中将“外观”选项选择为“自动”时,检测样式的最佳方法是什么?
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:@"AppleInterfaceStyle"];
BOOL darkModeOn = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );
即使从深色选项切换到自动外观选项后,darkModeOn 仍然是 yes/dark。
最佳答案
您需要将 AppleInterfaceStyle
与 macOS Catalina AppleInterfaceStyleSwitchesAutomatically
中引入的这个新值结合起来。
这里有一些伪代码解释如何:
theme = light //default is light
if macOS_10.15
if UserDefaults(AppleInterfaceStyleSwitchesAutomatically) == TRUE
if UserDefaults(AppleInterfaceStyle) == NIL
theme = dark // is nil, means it's dark and will switch in future to light
else
theme = light //means it's light and will switch in future to dark
endif
else
if UserDefaults(AppleInterfaceStyle) == NIL
theme = light
else
theme = dark
endif
endif
else if macOS_10.14
if UserDefaults(AppleInterfaceStyle) == NIL
theme = light
else
theme = dark
endif
endif
您可以在此处查看 macOS 示例应用程序:https://github.com/ruiaureliano/macOS-Appearance .
干杯💪
关于macos - 如何在 Catalina 的自动外观模式下检测 OSX 处于深色模式还是浅色模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57303286/