How To Detect And Test Dark Mode In SwiftUI

Since Apple introduced dark mode we need to adjust our apps to both schemes. Of course, one solution is to turn off dark mode but in most cases, it isn’t a solution.

In SwiftUI you can use Environment wrapper to check current scheme.

@Environment(\.colorScheme) var scheme

The scheme is an enum which can have one of two values, .light or .dark.

To detect it you can compare scheme value with contents using if statement.

var body: some View {
if scheme == .light {
    return Text(“Light scheme”)
else {
    return Text(“Dark scheme”)

By default, you’ll get a text: Light scheme. You can change the current scheme inside Environment Overrides (it’s that small square with two sliders below). You can find it between code and console.

Changing Interface Style you can test both cases, it changes during runtime as well. Remember to enable it at first.