3

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

Daniel Storm
  • 15,870
  • 6
  • 74
  • 133

3 Answers3

14

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>
Daniel Storm
  • 15,870
  • 6
  • 74
  • 133
  • I tried this coding 'didFinishLaunchingWithOptions', But it always shows 'unspecified'. – anas.p Feb 23 '17 at 10:50
  • @Anas that might be too early to check. Try moving it into your first VC's `viewDidLoad`. Make sure you have the key included in your info.plist also. – Daniel Storm Feb 23 '17 at 16:55
  • @AlizainPrasla I'm sure that wouldn't be possible without using a private API as its a system wide setting. – Daniel Storm Mar 29 '17 at 16:53
  • Lovely. UIUserInterfaceStyle was what was missing on the info.plist file of Kingfisher demo. I just added it (and assigned the info.plist target to the tvOS app) and now it starts in dark mode. – Michele Dall'Agata Aug 04 '18 at 08:56
1
if traitCollection.userInterfaceStyle == .dark {
}
Ryan
  • 8,298
  • 9
  • 42
  • 53
0

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
}
Jordan Hochstetler
  • 1,232
  • 2
  • 17
  • 27