SwiftUI: Navigate from a view with a navigationbackbutton to a fullscreen view

I am making a menu for my game using NavigationView and NavigationLink. I have three views as such:

struct MainMenuView: View {
    var body: some View {
        NavigationView {
            // relevant stuff
            NavigationLink(destination: CustomGameSettingsView()) {
                Text("Custom Game")
            }
        }
    }
}
struct CustomGameSettingsView: View {
    var body: some View {
        NavigationView {
            // ui to change game settings and stuff
            NavigationLink(destination: MyGameView(customSettings)) {
                Text("Play Game!")
            }
        }
    }
}

Note that the CustomGameSettingsView shows the navigationBarBackButton

struct MyGameView: View {
    var body: some View {
        myGameViews {
            // stuff
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarHidden(true)
        .navigationBarTitle(Text("myGame"))
        .edgesIgnoringSafeArea([.top, .bottom])

        // these are the things I have tried to get rid of the navigationBackButton
    }
    .onAppear() {
        self.navigationBarBackButtonHidden(true)
    } // another thing I tried
}

When I navigate from MainMenuView to CustomGameSettingsView, I gain a navigationBarBackButton which stays with me when I navigate to MyGameView. How do I get rid of that on the final navigation? I want the back button on the settings menu but I want my game to be fullscreen. Is NavigationView the wrong tool in this instance?

Thank you in advance.

>Solution :

The problem here is you use another NavigationView{} inside the CustomGameSettingsView. You don’t have to use another NavigationView because all your views (except MainMenuView) are already inside the NavigationView of the MainMenuView.

To fix this, replace the NavigationView inside the CustomGameSettingsView with a different container, then everything will work fine. Also, remove all your onAppear{}.

struct CustomGameSettingsView: View {
  var body: some View {
    VStack { //replace NavigationView with something else
        NavigationLink(destination: MyGameView()) {
            Text("Play Game!")
        }
    }
  }
}

Leave a Reply