This is a report of an issue that appears to be a regression regarding NavigationStack.
While investigating another issue [iOS18 beta2: NavigationStack, Views Being Popped Automatically] , I encountered this separate issue and wanted to share it.
In a NavigationStack with three levels: RootView - ContentView - SubView, tapping the Back button from the SubView returned to the RootView instead of the ContentView.
This issue is similar to one that I previously posted regarding iOS16.0 beta. https://developer.apple.com/forums/thread/715970
Additionally, there is no transition animation when moving from ContentView to SubView.
The reproduction code is as follows:
import SwiftUI
struct RootView2: View {
@State var kind: Kind = .a
@State var vals: [Selection] = {
return (1...5).map { Selection(num: $0) }
}()
@State var selection: Selection?
var body: some View {
if #available(iOS 16.0, *) {
NavigationStack {
NavigationLink {
ContentView2(vals: $vals, selection: $selection)
} label: {
Text("album")
}
.navigationDestination(isPresented: .init(get: {
return selection != nil
}, set: { newValue in
if !newValue {
selection = nil
}
}), destination: {
if let selection {
SubView2(kind: .a, selection: selection)
}
})
}
} else {
EmptyView()
}
}
}
struct ContentView2: View {
@Binding var vals: [Selection]
@Binding var selection: Selection?
@Environment(\.dismiss) private var dismiss
var body: some View {
list
.onChange(of: self.selection) { newValue in
print("changed: \(String(describing: newValue?.num))")
}
}
@ViewBuilder
private var list: some View {
if #available(iOS 16.0, *) {
List(selection: $selection) {
ForEach(self.vals) { val in
NavigationLink(value: val) {
Text("\(String(describing: val))")
}
}
}
}
}
}
//
struct SubView2: View {
let kind: Kind
let selection: Selection
var body: some View {
Text("Content. \(kind): \(selection)")
}
}