Skip to content
DeveloperMemos

Using SwiftUI's .colorScheme to Detect Light/Dark Mode

SwiftUI, Dark Mode, Light Mode2 min read

SwiftUI provides a simple and intuitive way to build user interfaces for iOS applications. One common requirement in modern app development is to support both light and dark modes. SwiftUI makes it easy to adapt your app's appearance based on the user's selected mode. In this article, we will explore how to use SwiftUI's .colorScheme modifier to detect the current mode and customize the user interface accordingly.

What is .colorScheme?

The .colorScheme modifier is a powerful tool provided by SwiftUI that allows you to dynamically adapt your app's appearance based on the user's chosen color scheme. The color scheme can be either light or dark, and you can use this modifier to adjust various visual elements of your app's UI to provide a consistent experience across different modes.

Detecting the Current Color Scheme

To detect the current color scheme in SwiftUI, you can use the @Environment property wrapper with the \.colorScheme key path. Here's an example:

1struct ContentView: View {
2 @Environment(\.colorScheme) var colorScheme
3
4 var body: some View {
5 Text("Hello, World!")
6 .foregroundColor(colorScheme == .dark ? .white : .black)
7 }
8}

In the above code, we access the colorScheme environment variable using @Environment(\.colorScheme). We can then use this variable to conditionally set the text color based on the current color scheme. If the color scheme is .dark, we set the text color to white; otherwise, we set it to black.

Adapting the User Interface

The ability to detect the current color scheme opens up various possibilities for customizing your app's user interface. Let's look at a few examples:

Customizing Colors

You can use the color scheme to define specific colors for different modes. For instance, you might want to use a different background color or accent color in dark mode. Here's an example:

1struct ContentView: View {
2 @Environment(\.colorScheme) var colorScheme
3
4 var body: some View {
5 VStack {
6 Text("Hello, World!")
7 .foregroundColor(colorScheme == .dark ? .white : .black)
8 .padding()
9 .background(colorScheme == .dark ? .black : .white)
10 .cornerRadius(8)
11 .shadow(radius: 2)
12
13 // Other UI components
14 }
15 }
16}

In this example, we set the text color to white and the background color to black when the color scheme is .dark. Otherwise, we use black text on a white background. By adapting the colors based on the color scheme, you can ensure that your app's UI looks consistent and appealing in both light and dark modes.

Using System Images

The system provides different sets of icons and images for light and dark modes. You can leverage the color scheme to use the appropriate system image based on the current mode. Here's an example:

1struct ContentView: View {
2 @Environment(\.colorScheme) var colorScheme
3
4 var body: some View {
5 VStack {
6 if colorScheme == .dark {
7 Image(systemName: "moon.fill")
8 } else {
9 Image(systemName: "sun.max.fill")
10 }
11
12 // Other UI components
13 }
14 .font(.system(size: 24))
15 }
16}

In this code snippet, we display a moon icon when the color scheme is .dark and a sun icon in light mode. By using system images, your app can seamlessly adapt to the user's chosen mode and provide a consistent visual experience.

Wrap Up

SwiftUI's .colorScheme modifier is a powerful tool that enables you to detect the current light or dark mode in your iOS app. By leveraging this feature, you can customize your app's appearance and provide a visually consistent experience across different modes. Whether it's adjusting colors, system images, or other UI elements, SwiftUI makes it easy to adapt your app to the user's preference!