[英]ScrollView conflicts with NavigationLink swipe-back gesture
I have a NavigationLink
view that contains a ScrollView
that mainly shows a list of contacts.我有一个
NavigationLink
视图,其中包含一个主要显示联系人列表的ScrollView
。 However, the ScrollView
sometimes register user's pop gesture as a scrolling gesture, thereby preventing the user swipe back out of the NavigationLink
view.但是,
ScrollView
有时会将用户的弹出手势注册为滚动手势,从而防止用户滑出NavigationLink
视图。
The general structure is like the follow:一般结构如下:
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink (destination: AnotherView()) {Text("enter")}
}
}
}
struct AnotherView: View {
var body: some View {
ScrollView {
LazyVStack(alignment: .leading) {
// ... a list of things
}
}
}
}
Is there anyway to tell ScrollView
to ignore the horizontal-ish drag gestures so that NavigationLink
can be swiped out properly and smoothly?有没有办法告诉
ScrollView
忽略水平方向的拖动手势,以便NavigationLink
可以正确流畅地滑出?
我通过将.gesture(DragGesture(minimumDistance: 20))
附加到我的ScrollView
使其工作,以便NavigationView
的弹出手势比ScrollView
的手势具有更高的优先级。
I had a similar problem with TabView.我对 TabView 有类似的问题。 The following extension solved it:
以下扩展解决了它:
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
// Allows swipe back gesture after hiding standard navigation bar with .navigationBarHidden(true).
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
viewControllers.count > 1
}
// Allows interactivePopGestureRecognizer to work simultaneously with other gestures.
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
viewControllers.count > 1
}
// Blocks other gestures when interactivePopGestureRecognizer begins (my TabView scrolled together with screen swiping back)
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
viewControllers.count > 1
}
}
Hope this will be helpful.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.