NavigationStack Being Automatically dismissed

Hello,

On the recent iOS / iPadOS 16, SwiftUI 4, and Xcode Version 14.0 beta (14A5228q), my app contains a NavigationStack within the content view; and the stack contains a ForEach that iterates over a list of fruits / items. The form loop outputs a navigation link, the label being a view, and the destination being another NavigationStack. And whenever I go to click on that link, the app shoots me back to the root view instantly. This seems to happen whenever I have any kind of content in the StackNavigation that's within the NavigationLink. I will post the code snippets below, along with a GIF showing the bug in action.

ContentView.Swift:

struct ContentView: View {
    @State private var isShowingSettings: Bool = false
    var fruits: [Fruit] = fruitsData

    var body: some View {
        NavigationStack {
            List(fruits.shuffled()) { fruit in
                NavigationLink(destination: FruitDetailView(fruit: fruit)) {
                    FruitRowView(fruit: fruit)
                        .padding(.vertical, 4)
                }
            }
            .navigationBarTitle("Fruits")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        isShowingSettings = true
                    }) {
                        Image(systemName: "slider.horizontal.3")
                    }
                    .sheet(isPresented: $isShowingSettings) {
                        SettingsView()
                    }
                }
            }
        }
    }
}

FruitDetailView.Swift:

// Code is shortened, but has the same bug as the actual code
struct FruitDetailView: View {
    var fruit: Fruit

    var body: some View {
        NavigationStack {
            ScrollView(.vertical, showsIndicators: false) {
                Text(fruit.name)
            }
            .edgesIgnoringSafeArea(.top)
        }
    }

Preview:

One thing that may be causing this is that fact that the FruitDetailView contains a NavigationStack, when it's parent, ContentView, already wraps it in one.

If removing the inner NavigationStack doesn't work, then maybe it's a bug.

I'm not sure how related this is but from the release notes:

A view-based NavigationLink in a List fails to update the visible selection of the list. (92193873)

I ran into basically the same problem. Below is the smallest sample app I came up with demonstrating this issue. It does not happen the first time you navigate in the stack, but on all subsequent times. I filed FB10662166 - feel free to reference in you feedbacks ;-)

import SwiftUI

struct SidebarEntry: Identifiable, Hashable {
  let id = UUID()
  var localizedName: String
}

let sidebarEntries = [
  SidebarEntry(localizedName: "Files"),
  SidebarEntry(localizedName: "Bookmarks"),
]

// MARK: - main -

@main
struct NewNavigationTestApp: App {
  @State private var sidebarSelection: SidebarEntry?

  var body: some Scene {
    WindowGroup {
      NavigationSplitView {
        List(sidebarEntries, selection: $sidebarSelection) { entry in
          NavigationLink("\(entry.localizedName)", value: entry)
        }
      } detail: {
        ZStack { // workaround
          if let sidebarSelection {
            if sidebarSelection.localizedName == "Files" {
              TestStackView()
            } else if sidebarSelection.localizedName == "Bookmarks" {
              Text("Bookmarks View")
            } else {
              Text("Unknown View")
            }
          } else {
            Text("No selection")
          }
        }
      }
    }
  }
}

// MARK: - FilesView -

struct TestStackView: View {
  var body: some View {
    NavigationStack {
      List(0 ..< 999) { idx in
        NavigationLink("\(idx)", value: idx)
      }
    }
    .navigationTitle("Numbers")
    .navigationDestination(for: Int.self) { idx in
      Text("Hello")
    }
  }
}

Persisting navigation path seems to get these to work as expected:

import SwiftUI

class NavigationModel: ObservableObject {
    @Published var presentedItems: [Int] = []
}


struct SidebarEntry: Identifiable, Hashable {
    let id = UUID()
    var localizedName: String
}

let sidebarEntries = [
    SidebarEntry(localizedName: "Files"),
    SidebarEntry(localizedName: "Bookmarks"),
]



// MARK: - main -


@main
struct NewNavigationTestApp: App {
    @State private var sidebarSelection: SidebarEntry?
    @StateObject var navigationModel =  NavigationModel()

    var body: some Scene {
        WindowGroup {
            NavigationSplitView {
                List(sidebarEntries, selection: $sidebarSelection) { entry in
                    NavigationLink("\(entry.localizedName)", value: entry)
                }
            } detail: {
                ZStack { // workaround
                    if let sidebarSelection {
                        if sidebarSelection.localizedName == "Files" {
                            TestStackView()
                        } else if sidebarSelection.localizedName == "Bookmarks" {
                            Text("Bookmarks View")
                        } else {
                            Text("Unknown View")
                        }
                    } else {
                        Text("No selection")
                    }
                }
            }
            .environmentObject(navigationModel)
        }
    }
}

// MARK: - FilesView -

struct TestStackView: View {
    @EnvironmentObject var navigationModel: NavigationModel

    var body: some View {
        NavigationStack(path: $navigationModel.presentedItems) {
            List(0 ..< 999) { idx in
                NavigationLink("\(idx)", value: idx)
            }
            .navigationTitle("Numbers")
            .navigationDestination(for: Int.self) { idx in
                Text("Hello \(idx)")
            }
        }
    }
}
NavigationStack Being Automatically dismissed
 
 
Q