【问题标题】:SwiftUI - Using Navigation View and Sheets correctlySwiftUI - 正确使用导航视图和表格
【发布时间】:2020-06-20 04:58:02
【问题描述】:

NavigationView 和 Sheet 有问题。我有以下流程: - 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")
        }
    }
}

【问题讨论】:

    标签: swiftui navigationbar navigationlink


    【解决方案1】:

    你只需要一个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)
        }
    }
    
    

    【讨论】:

    • 在工作表中没有导航按钮项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2022-12-12
    • 2023-01-16
    • 2020-08-08
    相关资源
    最近更新 更多