简体   繁体   中英

SwiftUI - Deal with regular/regular size classes on iPad

I need to make a different layout for iPad on landscape and portrait orientations, but size classes on iPad are always regular/regular. Recommended way to adapt interfaces by Apple is to use adaptive layout with size classes, but on iPad there is no difference between portrait and landscape mode.

Is there any way to present different layout on portrait and landscape orientation on iPad without detection device orientation?

As a example, on iPhone (compact/regular) will be shown as:

在此处输入图像描述

And on iPad, in landscape (regular/regular) will be shown as: 在此处输入图像描述

So, the goal is show the iPhone layout on iPad when is in portrait mode.

Thanks!

You can force apply horizontal size class depending on orientation. If I correctly understood your goal here is a demo of possible approach (tested with Xcode 12.1 / iOS 14.1):

struct DemoViewSizes: View {
    @Environment(\.horizontalSizeClass) var horizontalSizeClass

    @State private var orientation = UIDevice.current.orientation

    var body: some View {
        Text("root_view_here")
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                orientation = UIDevice.current.orientation
            }
            .environment(\.horizontalSizeClass, orientation == .portrait ? .compact : .regular)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM