SwiftUI:NavigationLink 和列表项中的显示工作表按钮同时工作

时间:2021-05-18 08:04:37

标签: swiftui swiftui-navigationlink

我有一个包含一些文本内容的列表项,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)
            }

        }
    }
}

List Item

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是将 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
        }
相关问题