链接SwithUI视图时显示多个导航栏

时间:2020-02-01 15:29:14

标签: swift xcode swiftui navigationbar navigationview

我有两个SwiftUI视图,它们是我与NavigationLink链接的独立文件:

View1:

struct ViewOne: View {

    var body: some View {
        NavigationView {
        VStack {
        NavigationLink(destination: ViewTwo()
       .navigationBarBackButtonHidden(true)
) {
                Text("Go to second view")
            }
.navigationBarTitle(Text("First View"), displayMode: .inline)
            }}}}

View2:

struct ViewTwo: View {

        var body: some View {
            NavigationView {
            VStack {
                    Text("Hello world")
                }
    .navigationBarTitle(Text("Second View"), displayMode: .inline)
                }}}

问题:在View2上,我不需要.navigationBarBackButton,因此我将其设置为在ViewOne中隐藏,但由于我将其设置为隐藏,因此它与ViewTwo上的现有NavigationBar混淆并显示多个NavigationBars

View2

如何在不破坏View2上现有的NavigationBar的情况下禁用navigationBarBackButton?

1 个答案:

答案 0 :(得分:0)

NavigationView在一个视图层次结构中应该仅为一个,因此只需在ViewTwo中将其删除:

struct ViewTwo: View {

        var body: some View {
            VStack {
               Text("Hello world")
            }
            .navigationBarTitle(Text("Second View"), displayMode: .inline)
        }
}