How to check if Dark Appearance is enabled tvOS

烂漫一生 提交于 2020-01-02 07:58:13

问题


How do I check if a user has enabled Dark Appearance on their Apple TV?


回答1:


Using UIUserInterfaceStyle, first available in tvOS 10, we can check what appearance the user has set.

For example:

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

Also, if you're updating from an Xcode 7/tvOS 9.0 project you will need to include UIUserInterfaceStyle in your info.plist. New projects created with Xcode 8 already have this key included.

<key>UIUserInterfaceStyle</key>
    <string>Automatic</string>



回答2:


if traitCollection.userInterfaceStyle == .dark {
}



回答3:


I wrote this extension in Swift 5:

extension UIViewController {
    var isDarkModeEnabled : Bool {
        get {
            return traitCollection.userInterfaceStyle == .dark
        }
    }
}

You can then call this in your UIViewControllers:

if self.isDarkModeEnabled {
    //Do something dark
} else {
    //Do something light
}


来源:https://stackoverflow.com/questions/39562862/how-to-check-if-dark-appearance-is-enabled-tvos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!