UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

1      

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 ....

1      

@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.

1      

@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.

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.