swiftui - SwiftUI Picker and NavigationLink Gesture Conflicts
问题描述
I have a situation when I have a picker inside of a NavigationLink. However, when changing the picker toggle it also triggers the NavigationLink and you are directed to a new screen. Here is some code:
enum OptionTypes:Int,CaseIterable {
case option1
case option2
var title:String {
switch self {
case .option1:
return "Option 1"
case .option2:
return "Option 2"
}
}
}
struct ContentView: View {
@State var pickerState:OptionTypes = .option1
@State var isActive:Bool = false
var body: some View {
NavigationView {
NavigationLink(destination: Destination(), isActive:$isActive) {
Picker("Options", selection: self.$pickerState) {
ForEach(OptionTypes.allCases, id: \.self) { type in
Text(type.title)
}
}
.pickerStyle(SegmentedPickerStyle())
.padding()
}
}
}
}
struct Destination:View {
var body: some View {
Text("Hello World")
}
}
The ideal situation is to have Picker drag and tap gestures not fire off the NavgiationLink. I've tried varying permutations of highPriorityGesture
and simultaneousGesture
i.e. something like:
Picker("Options", selection: self.$pickerState) {
ForEach(OptionTypes.allCases, id: \.self) { type in
Text(type.title)
}
}
.pickerStyle(SegmentedPickerStyle())
.highPriorityGesture(DragGesture(minimumDistance: 0.1, coordinateSpace: .global).onEnded { _ in
})
But this route doesn't seem to work. I also tried having the NavigationLink make use of isActive
. However, that also doesn't seem to be respected. It still navigates any way without me even making the isActive
true. I'm now not sure on how to have a picker function independently from the NavigationLink.
解决方案
You need to have NavigationLink
separated from Picker
in either way, like
Make invisible
NavigationLink(destination: Destination(), isActive:$isActive) { EmptyView() } Picker("Options", selection: self.$pickerState) { ForEach(OptionTypes.allCases, id: \.self) { type in Text(type.title) } } .pickerStyle(SegmentedPickerStyle()) .padding()
In background
Picker("Options", selection: self.$pickerState) { ForEach(OptionTypes.allCases, id: \.self) { type in Text(type.title) } } .pickerStyle(SegmentedPickerStyle()) .padding() .background( NavigationLink(destination: Destination(), isActive:$isActive) { EmptyView() })
推荐阅读
- javascript - Ajax Call 移除 UI 元素
- antlr4 - 如何根据ANTLR4中的关键字跳过输入
- python - 使用 Python Scrapy 无法抓取 BBC
- javascript - 将 .mp3 转换为 .ogg 作品/电报机器人/节点 js
- git - 如何将 10GB 的文件推送到 git?
- google-apps-script - 将日期从 d"-"mmm"-"yy 格式化为 "YYYY-MM-DD" 输出无效年份
- node.js - 部署 ReactJs + MySQL + NodeJs Express 项目
- python - pytest:在测试用例不可见的夹具中分配属性
- sql - SQL : 获取对应数据的 SQL 查询
- python - 将 Pandas 数据框导出到 Excel 时为特定单词着色