2

After adding a combined gesture to a view, a TextField inside the view would no longer respond when I would tap into it to change the text. I discovered this after adding a custom combined gesture - where I used a long press to start things before dragging. (Note: things still worked if just a drag gesture was added. Not sure what is particularly different between these two cases.)

The combined gesture:

let combined = longPressGesture.simultaneously(with: dragGesture)

The gesture was added to the view with:

.gesture(combined)
anorskdev
  • 1,867
  • 1
  • 15
  • 18

2 Answers2

1

I got things to work by adding an onTapGesture{} to the TextField. Didn’t have to put anything into the action. Seems like a side effect whose behavior could change in the future. Appreciate any comments on if this makes sense or other ways to handle.

TextField(“Enter Text”, text: $myText)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .onTapGesture {}
anorskdev
  • 1,867
  • 1
  • 15
  • 18
  • Update: I had the TextField inside a horizontal ScrollView. Discovered I had some other things inside the ScrollView not responding “properly” as well. Instead add the “empty” .onTapGesture{} to the HStack inside the ScrollView. Could then remove it from individual items inside the ScrollView/HStack - and make the code cleaner. – anorskdev Nov 05 '20 at 23:17
1

In case one would have this issue with a drag gesture, you can set the minimumDistance. This would still register the tap on the textfield to edit it.

DragGesture(minimumDistance: 30, coordinateSpace: .global)

Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling

Joosttk
  • 91
  • 1
  • 3