Logo for tanaschita.com

How to preview SwiftUI views in dark mode

Preview SwiftUI views in dark mode by setting the preferredColorScheme(_:).

08 Nov 2021 · 1 min read

It's really easy to preview SwiftUI views in dark mode. All we need to do is to call the preferredColorScheme(\_:) method on the view.

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().preferredColorScheme(.dark)
ContentView().preferredColorScheme(.light)
}
}
Preview a SwiftUI view in dark mode
Preview a SwiftUI view in dark mode
Image of books
Further reading: SwiftUI by Example
For further reading, check out the SwiftUI by Example book by Paul Hudson. It guides you through dozens of common coding problems with hands-on solutions.

Newsletter

Receive a monthly newsletter about the latest articles and tips.

Image of a reading marmot

Related tags

Written by

Articles with related topics

Latest articles and tips