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

寵の児 提交于 2021-01-20 14:37:11

问题


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()
    }
}

回答1:


You can't rotate the device in preview (yet), but 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")
        }
    }
}

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

    .environment(\.horizontalSizeClass, .regular)
    .environment(\.verticalSizeClass, .compact)



回答2:


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

View - IOS screen dimensions for different devices



来源:https://stackoverflow.com/questions/56625931/how-can-i-preview-a-device-in-landscape-mode-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!