Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Dark Appearance is enabled tvOS

Tags:

swift

tvos

tvos10

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

like image 280
Daniel Storm Avatar asked Dec 05 '22 16:12

Daniel Storm


2 Answers

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.

enter image description here

<key>UIUserInterfaceStyle</key>
    <string>Automatic</string>
like image 125
Daniel Storm Avatar answered Jan 06 '23 07:01

Daniel Storm


if traitCollection.userInterfaceStyle == .dark {
}
like image 40
Ryan Avatar answered Jan 06 '23 06:01

Ryan