SwiftUI: NavigationLink and ScrollView drag gesture colliding
Asked Answered
S

0

6

I'm trying to implement a UICollectionView like View in SwiftUI which basically works fine. But when I scroll/drag inside the scroll view to scroll down, the tap is recognized on the NavigationLink and then navigating to the detail view, even if I just wanted to scroll down.

Any ideas what might be causing this? Additional info: The whole NavigationView is opened from a .sheet from another view (as you might notice in the screenshot). I tried adding a "manual" link by setting the tag property on the link and setting the tag within a TapGesture, but this doesn't work either.

Here is a short example where the error can be reproduced. Scrolling down will activate a tap on one of the white rectangles.

View before drag gesture on scroll view

View during drag gesture, tap is already recognized on NavigationLink

Navigation view pushes to detail view after drag gesture

    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                    ForEach(0..<6) { i in
                        NavigationLink(destination: Text("Detail")) {
                            Rectangle()
                                .background(Color.red)
                                .frame(width: 365, height: 100, alignment: .center)
                        }
                    }
                }
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .navigationBarTitle("Items")
        }
    }
Samul answered 17/10, 2019 at 20:42 Comment(4)
Your code works as expected as a stand-alone view, just not presented in a sheet. This most likely means that Apple didn't expect a NavigationView to be used within .sheet(), and didn't test for weird effects like this. I'd file feedback (feedbackassistant.apple.com).Hartman
Definitely looks like a major oversight on Apples side. I've been trying to cancel the navigation link when a drag starts by doing .highPriorityGesture(DragGesture()) on the NavigationLink and this works but it also hijacks the scrollviews drag :/Koffler
Thanks everybody, I filed a feedback at AppleSamul
I think the issue is that the SwiftUI ScrollView doesn't delay it's content touches the way UIKit does: developer.apple.com/documentation/uikit/uiscrollview/…Koffler

© 2022 - 2025 — McMap. All rights reserved.