SwiftUI Preview Orientation Tutorial
in SwiftUI the preview can be displayed with different orientation using the previewInterfaceOrientation() modfier.. In this tutorial the preview will be displayed with the four different oriientations. portrait, portraitUpsideDown, landscapeLeft and landscapeRight. This tutorial is built for iOS15 and Xcode 13, which can be download 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 App template in the Application section and then click Next.
Enter SwiftUIPreviewOrientationTutorial as the Product Name, select SwiftUI as Interface, SwiftUI App as Life Cycle and Swift as Language. Deselect the Include Tests 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 ContentView_Previews struct to
struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() ContentView() .previewInterfaceOrientation(.landscapeLeft) ContentView() .previewInterfaceOrientation(.landscapeRight) ContentView() .previewInterfaceOrientation(.portraitUpsideDown) } }
The default preview interface orientation is portrait, so it isn’t required to specify it.
The preview for the landscapeleft orientation will look like this.
The preview for the landscaperight orientation will look like this.
The preview for the portraitupsidedown orientation will look like this.
The source code of the SwiftUIPreviewOrientationTutorial can be downloaded at the ioscreator repository on Github.













