3

NavigationView 和工作表有问题。我有以下流程: - ContentView:有打开 ContentView2 工作表的按钮 - ContentView2:有 NavigationLink,标题转到 ContentView3 - ContentView3:有 NavigationLink,没有标题,将用户定向到 ContentView2

但是,当我设置上述流程时,当用户在 ContentView2 和 ContentView3 之间来回切换时,我最终会得到堆叠的标题。当用户在两个视图之间来回切换时,我将如何防止这种情况并且只有 1 个标题?谢谢!

struct ContentView: View {
    @State var showSheet = false

    var body: some View {
        Button("Click"){
            self.showSheet.toggle()
        }
        .sheet(isPresented: $showSheet) {
            ContentView2()
        }
    }
}


struct ContentView2: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: ContentView3()){
                Text("Click Here")
            }
            .navigationBarTitle("Bar Title", displayMode: .inline)
        }
    }
}

struct ContentView3: View {
    var body: some View {
        NavigationLink(destination: ContentView2()){
            Text("Click Here")
        }
    }
}

问题截图

4

1 回答 1

4

你只需要一个NavigationView根目录,所以这里是更正的组件

struct ContentView: View {
    @State var showSheet = false

    var body: some View {
        Button("Click"){
            self.showSheet.toggle()
        }
        .sheet(isPresented: $showSheet) {
           NavigationView {    // only here !!
            ContentView2()
           }
        }
    }
}


struct ContentView2: View {
    var body: some View {
         NavigationLink(destination: ContentView3()){
             Text("Click Here")
         }
         .navigationBarTitle("Bar Title", displayMode: .inline)
    }
}

于 2020-06-20T06:37:04.203 回答