繁体   English   中英

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

[英]SwiftUI: Navigate from a view with a navigationbackbutton to a fullscreen view

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

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
}

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

先感谢您。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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