9

SwiftUI Preview Orientation Tutorial

 2 years ago
source link: https://www.ioscreator.com/tutorials/swiftui-preview-orientation-tutorial
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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.

Portrait Orientation

The preview for the landscapeleft orientation will look like this.

swiftui-preview-landscapeleft.png

The preview for the landscaperight orientation will look like this.

swiftui-preview-landscaperight.png

The preview for the portraitupsidedown orientation will look like this.

swiftui-preview-portrait-upsidedown.png

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK