0

我仍在使用 xcode 10.2.1,并且由于其他一些问题尚未升级到 xcode 11。现在我想检测使用 iOS 13 的用户是否选择了深色模式或浅色模式作为他们的应用设置。

根据苹果文档,如果开发人员通过以前的 xcode 构建应用程序,则应用程序默认处于轻量模式,这是我的情况,很好。

那么,有没有办法检测用户当前的外观模式。

我正在使用代码片段:

if #available(iOS 13.0, *) {
            guard(traitCollection.responds(to: #selector(getter: UITraitCollection.userInterfaceStyle)))
                else { return }

            let style = traitCollection.userInterfaceStyle

            switch style {
            case .light:
                print("light")
            case .dark:
                print("dark")
            case .unspecified:
                print("unspecified")
            @unknown default:
                print("unspecified")
            }

        }

但它总是返回未指定或轻。

4

1 回答 1

2

您可以使用此属性检查当前样式是否为暗模式:

if #available(iOS 13.0, *) {
    if UITraitCollection.current.userInterfaceStyle == .dark {
        print("Dark mode")
    }
    else {
        print("Light mode")
    }
}
于 2019-10-14T11:22:24.243 回答