How to Check If Dark Appearance Is Enabled Tvos

How to check if Dark Appearance is enabled tvOS

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.

Sample Image

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

How can I check whether dark mode is enabled in iOS/iPadOS?

SwiftUI

With the \.colorScheme key of an Environment variable:

struct ContentView: View {
@Environment(\.colorScheme) var colorScheme

var body: some View {
Text(colorScheme == .dark ? "In dark mode" : "In light mode")
}
}

Also, it automatically updates on the change of the environment color scheme.



UIKit

To check the current, all object those conform to UITraitEnvironment protocol, including all UIView subclasses and all UIViewConttroller subclasses have access to the current style:

myUIView.traitCollection.userInterfaceStyle == .dark
myUIViewController.traitCollection.userInterfaceStyle == .dark

To detect live changes of the style, here is the full detailed answer

Turn on system dark mode from app - Swift

  • First question: Impossible at this moment
  • Second Question: Answer of Tamás Sengel

You should check the userInterfaceStyle variable of UITraitCollection, same as on tvOS and macOS.

switch traitCollection.userInterfaceStyle {
case .light: //light mode
case .dark: //dark mode
case .unspecified: //the user interface style is not specified
}

You should use the traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) function of UIView/UIViewController to detect changes in the interface environment (including changes in the user interface style).

From Apple Developer Documentation:

The system calls this method when the iOS interface environment changes. Implement this method in view controllers and views, according to your app’s needs, to respond to such changes. For example, you might adjust the layout of the subviews of a view controller when an iPhone is rotated from portrait to landscape orientation. The default implementation of this method is empty.

System default UI elements (such as UITabBar or UISearchBar) automatically adapt to the new user interface style.

How to use dark mode in simulator iOS 13?

In Settings, scroll down to Developer and then Dark Appearance

Sample Image


Update

In addition to the above, there are now many other ways to enable dark appearance in the simulator, as shown in the many great answers below.

• Change Environment Overrides from Xcode (@AshCameron)

• Toggle Appearance A from the Simulator menu (@Shredder2794)

• Update from the command line using xcrun simctl ui booted appearance … (@blackjacx, @tadija)

• Programmatically using overrideUserInterfaceStyle = .dark (@thisIsTheFoxe)

• Specify UIUserInterfaceStyle in your info.plist (@DhavalGevariya)

• Use SimGenie from Curtis Herbert…  https://simgenie.app

UIActivityIndicatorView is black

In my case, my info.plist file was missing an entry called User Interface Style that is automatically added when creating newer Xamarin.tvOS projects that target tvOS 10 or above. When I added it and set it to Automatic (the other two options being Light or Dark), I began to see the behavior I expected.

I found this link to the Xamarin guide to User Interface Styles to be helpful: https://learn.microsoft.com/en-us/xamarin/ios/tvos/platform/user-interface-styles

Xcode / plist - run application always in Dark/Light theme on macOS

If you are looking for the full on SwiftUI version:

import SwiftUI

@main
struct SwiftUI_Football_GridApp: App {
var body: some Scene {
WindowGroup {
ContentView().preferredColorScheme(.light)
}
}
}


Related Topics



Leave a reply



Submit