This is a report of an issue that appears to be a regression regarding NavigationStack.
I have been aware of an issue where views are being automatically popped within NavigationView / NavigationStack since iOS 15, and it seems to be reoccurring in iOS 18.0 beta2.
Below is the reproducible code. Additionally, in my environment, this problem does not occur iOS 18 simulator, but it does happen on an iPhone XS Max(real device) with iOS 18 beta 2.
Environment:
- Xcode: Version 16.0 beta (16A5171c)
- iOS: 18.0 (22A5297f)
- iPhone: XS Max (real device)
import SwiftUI
@main
struct iOS16_4NavigationSample2App: App {
var body: some Scene {
WindowGroup {
NavigationStack {
NavigationLink {
ContentView()
} label: {
Text("Content")
}
}
}
}
}
enum Kind { case none, a, b, c }
struct Value: Hashable, Identifiable {
let id: UUID = UUID()
var num: Int
}
@MainActor
class ContentModel: ObservableObject {
@Published var kind: Kind = .a
@Published var vals: [Value] = {
return (1...5).map { Value(num: $0) }
}()
init() {}
}
struct ContentView: View {
@StateObject private var model = ContentModel()
@State private var selectedData: Value?
@State private var isShowingSubView = false
@Environment(\.dismiss) private var dismiss
init() {
}
var body: some View {
if #available(iOS 16.0, *) {
List(selection: $selectedData) {
ForEach(model.vals) { val in
NavigationLink(value: val) {
Text("\(val.num)")
}
}
}
.navigationDestination(isPresented: .init(get: {
selectedData != nil
}, set: { newValue in
if !newValue && selectedData != nil {
selectedData = nil
}
}), destination: {
SubView(kind: model.kind)
})
}
}
}
struct SubView: View {
init(kind: Kind) {
print("init(kind:)")
}
init() {
print("init")
}
var body: some View {
Text("Content")
}
}
This code was shared in a different issue [iOS 16.4 NavigationStack Behavior Unstable].