I have a NavigationView with a Form
containing a Picker
and a NavigationLink
, and want to use navigationViewStyle(StackNavigationViewStyle())
. When doing a fresh launch in portrait mode it behaves as expected. But after rotating the device to landscape and back to portrait and tapping the NavigationLink it loads multiple copies of the destination view and then returns back to the original view, as seen here:
UI gone berserk
This happens booth on physical device and in simulator.
If I remove navigationViewStyle(StackNavigationViewStyle())
this problem doesn’t occur, but I would prefer to have it.
I’ve boiled down the problem to this code sample. This is run in a fresh SwiftUI project, with no changes made to the standard SceneDelegate.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView{
Form{
NavigationLink(destination: DestinationView()) {
Text("NavigationLink")
}
}.navigationBarTitle("ContentView")
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct DestinationView: View {
var body: some View {
Text("Destination")
.navigationBarTitle("DestinationView")
}
}
After rotating the device to landscape and back and then tapping the NavigationLink, this is what it looks like in the visual debugger:
Visual debugger
There are duplicates of the view behind each other. With every rotation back and forth more are added.
I’ve googled and searched Stackoverflow without finding a solution.
Update: With Xcode 12 this bug is fixed.
With Xcode 11.3.1 there seems to be a bug in Form
, making a new copy of the view with every orientation change of the device.
Sample code:
import SwiftUI
struct ContentView: View {
var body: some View {
Form{
Text("Text")
}
}
}
Visual debugger at beginning, rotate left and then rotate right:
A Form
automatically adopts the GroupedListStyle
and it seems that it’s actually there the bug is. Because a List
with GroupedListStyle
shows the same behaviour in the visual debugger.
import SwiftUI
struct ContentView: View {
var body: some View {
List{
Text("Text")
}.listStyle(GroupedListStyle())
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With