Swift and SwiftUI tutorials for Swift Developers

How to Enable and Test your App using Dark Mode in Xcode

Since the release of iOS 13, Apple has allowed users to choose between light mode and dark mode.

When the user selects Dark Mode, both the system and apps use a darker color palette for all screens and views. As an app developer, you should ensure that your apps also support Dark Mode. There are several ways to test your app in Dark Mode.

Using the Preview Macro

The first way we’ll look at is using the Preview macro in Xcode. Insert the following code into ContentView.swift

#Preview("Dark Mode") {
ContentView()
.preferredColorScheme(.dark)
}

The preferredColorScheme modifier allows us to switch to Dark Mode by passing the value .dark. With this update, you can view your app in dark mode.

Using Variants in Xcode

You can use the Variants option in Xcode below the canvas preview.

When you enable the Color Scheme option, Xcode will provide you with previews in both light and dark modes.

Using the iOS Simulator in Xcode

The third way to preview your app in dark mode is through simulators. After launching your app in a simulator, you can click the Environment Overrides button to change the appearance from light to dark. Once you activate the toggle, the simulator will be set to dark mode.

Using the Xcode iOS Simulator Settings

A final method to test dark mode is to adjust your simulator settings. In the simulator, simply go to Settings > Developer. Toggle the “Dark Appearance” option to activate dark mode.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

How to Create a Rounded Corners View in SwiftUI

Next Article

How to add custom swipe action buttons to a List row in SwiftUI

Related Posts