iosios13ipadosios-darkmode

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


Starting from iOS/iPadOS 13, a dark user interface style is available, similar to the dark mode introduced in macOS Mojave. How can I check whether the user has enabled the system-wide dark mode?


Solution

  • 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