SwiftUI:NavigationLink 和列表项中的显示 Sheet 按钮同时工作
SwiftUI : NavigationLink and the show Sheet Button in the List item works same time
我有一个包含一些文本内容的列表项,navigationLink
和要显示的按钮 .sheet
。当我单击导航 link 或显示 sheet 按钮时导航目的地和 sheet appear.How 来避免这种行为?
注意:这是最少的可生成代码。
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)
}
}
}
}
一个可能的解决方案是将 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
}
我有一个包含一些文本内容的列表项,navigationLink
和要显示的按钮 .sheet
。当我单击导航 link 或显示 sheet 按钮时导航目的地和 sheet appear.How 来避免这种行为?
注意:这是最少的可生成代码。
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)
}
}
}
}
一个可能的解决方案是将 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
}