3

I want to have a root NavigationStack that lets the user navigate around a SwiftUI app, including to a TabView with tabs that have their own navigation stack. Unfortunately, this seems to not work at all (xcode 14.2, iOS 16).

The following example demonstrates the issue. When you attempt to navigate inside the tab view navigation stack, the tabs disappear and then the app goes into a broken state where navigation basically stops working entirely.

import SwiftUI

struct TabsView: View {
    var body: some View {
        TabView {
            NavigationStack {
                ZStack {
                    NavigationLink("Navigate to child tab", value: 1)
                }
                .navigationDestination(for: Int.self) { screen in
                    Text("Tab child \(screen)")
                }
            }
            .tabItem {
                Label("Screen 1", systemImage: "house")
            }
            
            Text("Screen 2")
                .tabItem {
                    Label("Screen 2", systemImage: "house")
                }
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationStack {
            VStack {
                NavigationLink("Show Tabs", value: "tabs")
            }
            .navigationDestination(for: String.self) { screen in
                if screen == "tabs" {
                    TabsView()
                } else {
                    Text("?")
                }
            }
        }
    }
}

How can I make this work?

8
  • does this answer you question. Swift navigation bar not appearing.
    – Qazi Ammar
    Dec 14, 2022 at 6:03
  • 1
    Apple doesn’t want a TabView below a NavigstionView/Stack it must always be at the top this answer elaborates a little Dec 14, 2022 at 7:57
  • But it works fine if I use the old NavigationView instead of the newer NavigationStack, so I don't think that's really the issue.
    – Greg Ennis
    Dec 14, 2022 at 10:56
  • It just appears to look fine on iPhone the issues are clear if you try on iPad (Double column) and try to assign navigation title's, you end up having to hide navigation bars, etc. Apple has gone out of their way to enforce that they shouldn't be related. Dec 14, 2022 at 12:50
  • 1
    You can also create your own tab view and all the issues are gone, it only takes a few lines of code. Dec 14, 2022 at 13:00

2 Answers 2

0

Use NavigationView in ContentView. Apple has problems with NavigationStack in hierarchy of NavigationStack.

1
0

Found solution for this one!

I am placing "top" NavigationStack with Color.clear initial view right after TabView in ZStack.

ZStack {
    tabBarView
        .zIndex(0)
    
    NavigationStack(path: $coordinator.topPath) {
        Color.clear
            .navigationDestination(for: CoordinatorService.Step.self) { destination in
                coordinator.resolve(pathItem: destination)
            }
    }
    .zIndex(1)
    .allowsHitTesting(!coordinator.topPath.isEmpty)
    .introspect(.navigationStack, on: .iOS(.v16, .v17)) {
        $0.viewControllers.forEach { controller in
            controller.view.backgroundColor = .clear
        }
    }
}
.environmentObject(coordinator)

We have to use Introspect library to remove that's NavigationStack's white background, because SwiftUI don't bring that possibility.

Also I am disabling hit testing on top navigation view when there are no views on top.

Check out full example project here:

https://github.com/thekingofsofa/TabNavigationStack/tree/main

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.