SwiftUI Preview Multiple Devices Tutorial

The preview can be used to test and display multiple devices to view the layout differences. In this tutorial . some views will be displayed on two different iOS devices in the preview. SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be downloaded at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select the Single View App template, and then click Next. Enter SwiftUIPreviewDevicesTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next. Choose a location to save the project on your Mac.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Change the code inside the ContentView struct to

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Multiple Devices")
                .font(.largeTitle)
            Circle()
                .foregroundColor(Color.blue)
                .padding(.horizontal ,10)
            
            Text("Testing Preview")
                .font(.title)
            Spacer()
        }
    }
}

A circle surrounded by two text views are displayed. Next change the preview struct to

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
           ContentView()
              .previewDevice(PreviewDevice(rawValue: "iPhone SE"))
              .previewDisplayName("iPhone SE")

           ContentView()
              .previewDevice(PreviewDevice(rawValue: "iPhone 11 Pro Max"))
              .previewDisplayName("iPhone 11 Pro Max")
        }
    }
}

This will create a group of two devices in the Preview, which will be displayed in the preview.

The source code of the SwiftUIPreviewDevicesTutorial can be downloaded at the ioscreator repository on Github.