1

I've looked around but haven't seen any info on this. If I want to have multiple navigation stacks in SwiftUI, and have multiple navigation links that add to different navigation stacks, how can I do this? In my use case I use a tab view, so I would be something like:

NavigationStack(path: $globalNavigationStack) {
  TabView {
    PrimaryView()
    SecondaryView()
  }
}

Then maybe in PrimaryView:

struct PrimaryView: View {
  var body: some View {
    NavigationStack(path: $localNavigationStack) {
       NavigationLink(value: SomeType.self) {
         Button("This should add to local stack")
       }
       .navigationDestination(for: SomeType.val) { someType in
          ViewOnLocalStack() 
       }

       NavigationLink(value: SomeType.self) {
         Button("This should add to global stack")
       }
       .navigationDestination(for: SomeType.val) { someType in
          ViewOnGlobalStack() 
       }
    }
  }
}

But the second NavigationLink will add to the local stack. Assuming I have access to the global path, how can I make the second one add to global path? What I tried was this:

NavigationStack(path: $globalNavigationStack) {
  TabView {
    PrimaryView()
    SecondaryView()
  }
}
.navigationDestination(for: SomeType.val) { someType in
    ViewOnGlobalStack() 
}

along with removing the onDestination from the second link, but the console complained that the navigation link had no corresponding onDestination. Not sure if I've seen anyone else have a similar situation.

Nicolas Gimelli
  • 695
  • 7
  • 19
  • 1
    Each `tab` can have a `NavigationStack` but a `TabView` shouldn't be inside one. They should be "mutually exclusive" per the human interface guidelines. that global stack goes against Apple's design – lorem ipsum May 22 '23 at 18:51
  • Did you come up with a solution? Nested Navigation by UINavigaitonViewController perfectly works on UIKit. But I have an issue with "pop" and "pop to root" in SwiftUI. – Amirca Jul 12 '23 at 18:29

0 Answers0