简体   繁体   中英

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.

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!")
        }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM