How to create multiple screen previews in SwiftUI

Today I learned that you can actually create multiple Xcode screen previews in SwiftUI.

Assuming you have a SwiftUI View named ContentView. In the PreviewProvider, create a Group and init multiple ContentView() children inside of it. For example:

 1struct ContentView_Previews: PreviewProvider {
 2    static var previews: some View {
 3        Group {
 4            ContentView()
 5                .previewDevice(PreviewDevice(rawValue: "iPhone 11"))
 6                .previewDisplayName("iPhone 11") // Optional
 7            ContentView()
 8                .previewDevice(PreviewDevice(rawValue: "iPhone 8"))
 9                .previewDisplayName("iPhone 8 Dark")
10                .environment(\.colorScheme, .dark)
11                .environment(\.sizeCategory, .accessibilityLarge)
12            ContentView()
13                .previewDevice(PreviewDevice(rawValue: "iPhone SE"))
14                .previewDisplayName("iPhone SE")
15        }
17    }

This will display the View that you have created in 3 previews with the following order:

  1. Display it in iPhone 11
  2. Display it in iPhone 8 with dark mode enabled and large accessibility size font set
  3. Display it in iPhone SE

Here’s how the above code will look like in action

Xcode 11 Multiple Previews

This is going to be super useful when you want to quickly preview your View in different screen sizes, accessibility mode or color schemes. Neat huh?