BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

How can I prevent nested NavigationViews in SwiftUI?

Forums > SwiftUI

As the title says, I’m having issues with nested NavigationLink calls resulting in views with multiple “Back” buttons.

So, if we have ContentView leading to View B via a navigation link, when View B opens View C with another NavigationLink, View C has two, nested back buttons.

At the top level, the back button leads back to ContentView, and the next level leads back to View B.

In View C, I only want the back button that takes the user back to View B. Is there a way to make this happen, or is this an instance where I should be using sheets?

Thank you for your time, Zach As the title says, I’m having issues with nested NavigationLink calls resulting in views with multiple “Back” buttons.

So, if we have ContentView leading to View B via a navigation link, when View B opens View C with another NavigationLink, View C has two, nested back buttons.

At the top level, the back button leads back to ContentView, and the next level leads back to View B.

In View C, I only want the back button that takes the user back to View B. Is there a way to make this happen, or is this an instance where I should be using sheets?

Thank you for your time, Zach As the title says, I’m having issues with nested NavigationLink calls resulting in views with multiple “Back” buttons.

So, if we have ContentView leading to View B via a navigation link, when View B opens View C with another NavigationLink, View C has two, nested back buttons.

At the top level, the back button leads back to ContentView, and the next level leads back to View B.

In View C, I only want the back button that takes the user back to View B. Is there a way to make this happen, or is this an instance where I should be using sheets?

Thank you for your time, Zach As the title says, I’m having issues with nested NavigationLink calls resulting in views with multiple “Back” buttons.

So, if we have ContentView leading to View B via a navigation link, when View B opens View C with another NavigationLink, View C has two, nested back buttons.

At the top level, the back button leads back to ContentView, and the next level leads back to View B.

In View C, I only want the back button that takes the user back to View B. Is there a way to make this happen, or is this an instance where I should be using sheets?

Thank you for your time, Zach

   

Hi, just need to make sure when you navigate from one view to other, there are no more then one NavigationView , this usually happens when we end up having NavigationView in one View , go to other View and there is another NavigationView in there, get rid of those ....

   

@Bnerd  

I had similar query like yours in the past. My rookie way to overcome was to create a separate class that acts as a ViewRouter (name doesn't matter) that has an enum and the ContentView switches through the enum.

See sample code below and give it a try: Router:

import SwiftUI

class ViewRouter: ObservableObject {
    @Published var currentPage: Page = .🏈
}

enum Page {
     case 🏈
     case 🏀
     case 🎾
     case 🚧
 }

ContentView:

import SwiftUI
struct ContentView: View {
    @StateObject var viewRouter: ViewRouter //Instance of our Class

    var body: some View {
        NavigationView {
            GeometryReader { geo in
                ZStack {
                    Color.blue.opacity(0.50).ignoresSafeArea()
                        switch viewRouter.currentPage {
                         case .🏈:
                            Text("🏈").font(.system(size: 250))
                         case .🏀:
                            ChildView() // Or whatever view you want
                         case .🎾:
                            ListView() // Or whatever view your want
                         case .🚧:
                            Text("🚧").font(.system(size: 250))
                        }
                    VStack{
                        Spacer()
                        HStack(alignment: .center, spacing: 30) {
                            Text("🏈").onTapGesture { viewRouter.currentPage = .🏈 }
                            Text("🏀").onTapGesture { viewRouter.currentPage = .🏀 }
                            Text("🎾").onTapGesture { viewRouter.currentPage = .🎾 }
                            Text("🚧").onTapGesture { viewRouter.currentPage = .🚧 }
                            Image(systemName: "list.bullet").foregroundColor(.white).onTapGesture { showMenuOptions.toggle() }
                        }
                        .padding()
                        .font(.largeTitle)
                    }
                }
                .navigationTitle("My sample App")
                .navigationBarHidden(true)

            }
        }
    }
}

Of course in the above solution you have a tab version of an App, tweaking around will may give you what you are looking for.

   

@DarkhorseStudios Are you able to limit your app to iOS16 or later, or do you need to have the target set to iOS 15.x or even 14.x?

If you can set the app to use iOS16 as the target, then consider using the NavigationStack, and creating a @State variable for the [path]. You can manipulate [path] programmatically (deep links, state restoration and other programmatic navigation).

There is some basic info in the Xcode Quick Help under 'navigationstack' for SwiftUI.

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.