小编典典

SwiftUI:从带有导航返回按钮的视图导航到全屏视图

all

NavigationView我正在使用and为我的游戏制作菜单NavigationLink。我有这样的三个观点:

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

请注意,CustomGameSettingsView显示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
}

当我从MainMenuViewto导航时,当我导航到 时CustomGameSettingsView,我会得到一个navigationBarBackButton留在我身边的MyGameView。我如何在最终导航中摆脱它?我想要设置菜单上的后退按钮,但我希望我的游戏是全屏的。在这种情况下是NavigationView错误的工具吗?

先感谢您。


阅读 97

收藏
2022-08-17

共1个答案

小编典典

这里的问题是你NavigationView{}CustomGameSettingsView. 您不必使用另一个NavigationView ,因为您的所有视图( MainMenuView除外)都已经在MainMenuView.

要解决此问题,请将其中的NavigationView替换为不同的容器CustomGameSettingsView,然后一切正常。另外,删除所有onAppear{}.

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