Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I preview a device in landscape mode in SwiftUI?

How can I preview a device in landscape mode in SwiftUI?

I just have a simple preview like this:

struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
like image 819
Felipe Peña Avatar asked Jun 17 '19 06:06

Felipe Peña


People also ask

How do I create a preview in SwiftUI?

There are two shortcuts that you should remember. Both of them will make your life easier during the development cycle of your SwiftUI views. Cmd + Option + Enter shows or hides previews. Cmd + Option + P runs the previews.

How do I change the orientation of Xcode?

You can press Command + Arrow Key to change the iOS simulator screen orientation.

What is var body some view in SwiftUI?

'some' means opaque type. In SwiftUI, View is declared as a protocol @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) public protocol View { /// The type of view representing the body of this view.

What is content view in SwiftUI?

ContentView is the main view. Its job is to define which views compose our app. In here, we have a single view, Text . If you run this in Xcode, this is what the app will look like: Notice the additional code after the ContentView struct: this is how we tell Xcode what to display in the preview panel on the right.


3 Answers

Xcode 13

Now you can preview in landscape mode with .previewInterfaceOrientation modifier to make it landscapeLeft or landscapeRight.

⚠️ The following image from WWDC21 uses horizontal and vertical that is NOT available in Xcode 13 beta!

enter image description here


Old but not Useless method:

You can set the size to any landscape size you want for PreviewProvider:

struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
           .previewLayout(.fixed(width: 1024, height: 768))
           // iPad Mini landscape size
    }
}

This is iPad Mini landscape mode size.

Update: Xcode detects the device associated to the preview from the selected simulator at the top left of the IDE and it and will apply the safe area as you expected for some iPads and iPhones landscape mode.

💡 Master-Detail Demo

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Master")
            Text("Detail")
        }
    }
}

Demo

Also, you can play with these two modifiers as your needs:

    .environment(\.horizontalSizeClass, .regular)
    .environment(\.verticalSizeClass, .compact)
like image 71
Mojtaba Hosseini Avatar answered Oct 19 '22 03:10

Mojtaba Hosseini


ContentView().previewLayout(PreviewLayout.fixed(width:568,height:320))

View - IOS screen dimensions for different devices

like image 4
C Williams Avatar answered Oct 19 '22 02:10

C Williams


iOS 15+ / Xcode 13+

Starting from iOS 15 we can use previewInterfaceOrientation:

struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
            .previewInterfaceOrientation(.landscapeRight)
    }
}
like image 2
pawello2222 Avatar answered Oct 19 '22 04:10

pawello2222