In our app we observe the size class changes using @Environment(\.verticalSizeClass)
and @Environment(\.horizontalSizeClass)
. This works fine and we achieve the expected layout when the device was rotated.
The issue is when the app navigates to the other screen. If I am on MainScreen
in portrait mode then navigate to DetailScreen
then rotate the device to landscape the app go back to MainScreen
. It seems that the NavigationLink
was destroyed when the app redraw the MainScreen
due to the rotation in DetailScreen
.
Below is simplified code similar to what we use:
struct MainScreen: View {
@Environment(\.verticalSizeClass) var verticalSizeClass
@Environment(\.horizontalSizeClass) var horizontalSizeClass
var body: some View {
NavigationView {
if horizontalSizeClass == .regular && verticalSizeClass == .compact {
VStack {
// other views..
NavigationLink(
destination: { DetailScreen() },
label: { Text("Go to Details Screen") }
// other views..
}
} else {
HStack {
// other views..
NavigationLink(
destination: { DetailScreen() },
label: { Text("Go to Details Screen") }
// other views..
}
}
}
.navigationViewStyle(.stack)
}
}
struct DetailScreen: View {
@Environment(\.verticalSizeClass) var verticalSizeClass
@Environment(\.horizontalSizeClass) var horizontalSizeClass
var body: some View {
Text("Details Screen")
// Some views that also changes layout base on the verticalSizeClass and horizontalSizeClass
}
}
Does anyone encounter this issue and able to resolve it? Our app support from iOS15.