Compare the behavior of this two following code snippet; target iOS 14.4 iPad(4th gen) simulator, Xcode Version 12.4 (12D4e):
First:
struct experimental: View {
@State private var selection: Int? = 0
var body: some View {
NavigationView {
VStack {
ForEach(0 ..< 5) { item in
NavigationLink(
destination: Text("item \(item)"),
tag: item,
selection: $selection) {
Text("item \(item)")
}
}
}
Text("Hello, World!")
}
}
}
Second:
struct experimental: View {
@State private var selection: Int? = 0
var body: some View {
NavigationView {
List(0 ..< 5) { item in
NavigationLink(
destination: Text("item \(item)"),
tag: item,
selection: $selection) {
Text("item \(item)")
}
}
Text("Hello, World!")
}
}
}
The first example run as expected in simulator, the app initiate displaying default selection(item 0). However, the second example the app initiate displaying “Hello, World!” Text. When back button tapped on top left, it will return to default selection(item 0) then menu. The same unexpected behavior happened when we add another Text("Hello, World!") view to make it triple column navigation, middle and last column initiate to “Hello, World!” Text, but when back button on top tapped, middle column changed to default selection(item 0). It behave differently when List view used to wrap NavigationLink inside NavigationView.
Update: Problem occur on iPad DoubleColumnNavigationViewStyle() and new Three column NavigationView. Works fine for StackNavigationViewStyle