【问题标题】:SwiftUI : NavigationLink and the show Sheet Button in the List item works same timeSwiftUI:NavigationLink 和列表项中的显示工作表按钮同时工作
【发布时间】:2021-05-18 08:04:37
【问题描述】:

我有一个带有一些文本内容的列表项,navigationLink 和显示.sheet 的按钮。当我单击任一导航链接或显示工作表按钮时,导航目标和工作表都会出现。如何避免这种行为?

注意:这是最少的可生产代码。

struct ContentView: View {
    @State var shouldSheetShow:Bool = false;

    var body: some View {
        NavigationView{
            List{
                VStack(alignment: .leading){
                    Text("Some Text Stuff")
                    NavigationLink(
                        destination: Text("navigation link"),
                        label: {
                            Text("Navigate To some view")
                                .background(Color.green)
                        })

                    Button(action: {
                        self.shouldSheetShow = true
                    }, label: {
                        HStack{
                            Text("Show sheet")
                        }
                        .background(Color.blue)
                        .sheet(isPresented: $shouldSheetShow, content: {
                            Text("sheet")
                        })

                    })
                }
                .frame(width: 300, height: 150, alignment: .center)
                .background(Color.gray)
            }

        }
    }
}

【问题讨论】:

    标签: swiftui swiftui-navigationlink


    【解决方案1】:

    一种可能的解决方案是将Button 替换为Text 加上onTapGesture

    替换

    Button(action: {
            self.shouldSheetShow = true
        }, label: {
            HStack{
                Text("Show sheet")
            }
            .background(Color.blue)
            .sheet(isPresented: $shouldSheetShow, content: {
                Text("sheet")
            })
    
        })
    

    Text("Show sheet")
            .background(Color.blue)
            .sheet(isPresented: $shouldSheetShow, content: {
                Text("sheet")
            })
            .onTapGesture {
                self.shouldSheetShow = true
            }
    

    【讨论】:

    • 它有效,你能解释一下这个问题的理论原因吗?我的意思是为什么我们不能在列表项中使用我的组合。因为我的组合不在列表中时可以正常工作。
    • 老实说,我不知道正确的原因,但我想特别是在 List 中,NavigationItem 的可点击区域扩展到整个 cell 并影响 Button 区域.
    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多