macos - 如何在 Catalina 的自动外观模式下检测 OSX 处于深色模式还是浅色模式

标签 macos cocoa macos-catalina

我的 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/

相关文章:

c# - WPF到跨平台开发,最强的跨平台UI框架是什么?

objective-c - Xcode 4 内置帮助您自己的文件

macos - 带有 Split View的 Swift 中的源列表边栏实现

c++ - 代码在带有警告的 g​​++ 上编译,但在 clang3.1(Xcode 4.3.3) 上给出相同代码的错误

objective-c - 使用热键切换 NSStatusItem 的菜单打开/关闭 - 代码执行排队/阻止

flutter - zsh:找不到命令: flutter

apache - 在 OSX 中配置我的 Apache 虚拟主机的正确位置在哪里?

iphone - IOS + phone-gap 应用程序开发中的 db 文件实用程序

css - Cocoa WebKit - 检测 CSS 翻转/悬停

macos - PKG 安装程序似乎签名正确,但在 Catalina 上仍然显示 "unidentified developer"