NEW: Get your ticket for Hacking with Swift Live 2019! >>

< How to preview your layout at different Dynamic Type sizes   How to preview your layout in different devices >

How to preview your layout in light and dark mode

Most of Apple’s operating systems support both light and dark mode user interfaces, so it’s no surprise that SwiftUI has support for this functionality built right in.

Even better, once you’ve designed your interface Xcode allows you to preview your layouts in either color scheme by setting the \.colorScheme environment value in your preview.

For example, this shows a preview using dark mode:

#if DEBUG
struct ContentView_Previews : PreviewProvider {
   static var previews: some View {
      Group {
         ContentView()
            .environment(\.colorScheme, .dark)
      }
   }
}
#endif

If you want to see both light and dark mode side by side, place multiple previews in a group, like this:

#if DEBUG
struct ContentView_Previews : PreviewProvider {
   static var previews: some View {
      Group {
         ContentView()
            .environment(\.colorScheme, .light)

         ContentView()
            .environment(\.colorScheme, .dark)
      }
   }
}
#endif

Tip: If your preview is zoomed right in, you should either scroll around or zoom out to the other previews.

HACKING WITH SWIFT LIVE This July is a new two-day event where you'll be inspired by great speakers on day one then learn all the amazing new features from WWDC on day two – click here for more information and tickets.

< How to preview your layout at different Dynamic Type sizes   How to preview your layout in different devices >
MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>