如何检查是否启用了黑暗外观tvOS

Dan*_*orm 3 swift tvos tvos10

如何检查用户是否在Apple TV上启用了Dark Appearance?

Dan*_*orm 14

使用首先在tvOS 10中提供的UIUserInterfaceStyle,我们可以检查用户设置的外观.

例如:

func checkInterfaceStyle() {
    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")
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,如果您从Xcode 7/tvOS 9.0项目更新,则需要包含UIUserInterfaceStyle在您的info.plist.使用Xcode 8创建的新项目已包含此密钥.

在此输入图像描述

<key>UIUserInterfaceStyle</key>
    <string>Automatic</string>
Run Code Online (Sandbox Code Playgroud)