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