简体   繁体   English

SwiftUI - 处理 iPad 上的常规/常规尺寸类

[英]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.我需要在横向和纵向上为 iPad 制作不同的布局,但 iPad 上的尺寸类始终是常规/常规的。 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. Apple 推荐的适配界面的方法是使用具有大小类的自适应布局,但在 iPad 上,纵向和横向模式没有区别。

Is there any way to present different layout on portrait and landscape orientation on iPad without detection device orientation?有没有办法在没有检测设备方向的情况下在 iPad 上呈现不同的纵向和横向布局?

As a example, on iPhone (compact/regular) will be shown as:例如,在 iPhone (compact/regular) 上将显示为:

在此处输入图像描述

And on iPad, in landscape (regular/regular) will be shown as:在 iPad 上,横向(常规/常规)将显示为: 在此处输入图像描述

So, the goal is show the iPhone layout on iPad when is in portrait mode.因此,目标是在纵向模式下在 iPad 上显示 iPhone 布局。

Thanks!谢谢!

You can force apply horizontal size class depending on orientation.您可以根据方向强制应用水平尺寸 class。 If I correctly understood your goal here is a demo of possible approach (tested with Xcode 12.1 / iOS 14.1):如果我正确理解您的目标,这里是一个可能方法的演示(使用 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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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