This is an archived post from my old WordPress blog. Some code examples may reference older versions of Swift or SwiftUI.

How to Detect Dark Mode in SwiftUI

July 20, 2020 By Thomas Prezioso Jr 1 min read

SwiftUI makes it really simply to detect when dark mode is enabled. We simply have to add a @Enviroment variable and use .colorScheme property to scan the settings on our device and see if dark mode is enabled.

Let’s take a look at the example below.

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

var body: some View {
    ZStack {
        Color(colorScheme == .light ? .blue : .red)
        Text("Hello, World!")
    }
}

}

In the code above we are creating the @Environment variable to see if our device is in dark mode. Then inside of our body view we are setting the background color to red if its in dark mode or blue if its not in dark mode by using our colorScheme variable inside of a ternary operator.

A great use case for this is if you want to support different custom UI’s for when the users device is in dark mode.

🌃 Happy Coding! 🌃


Share this post
Post on X Share on LinkedIn

Back to top

© 2025 Thomas Prezioso Jr. All rights reserved.

Built with Swift using Ignite