Home:ALL Converter>iOS 13 Segmented Control: Remove swipe gesture to select segment

iOS 13 Segmented Control: Remove swipe gesture to select segment

Ask Time:2019-10-01T09:51:29         Author:ellek

Json Formatter

TLDR: How to remove the swipe/pan gesture recognizer for UISegmentedControl on iOS 13?

Hi, on iOS 13, lots changed with UISegmentedControl. Mostly, the changes were appearance-based. But there is another functionality change that is messing up my app.

On iOS 13, with UISegmentedControls, you can now swipe/pan to change the selected segment in addition to touching the segment you would like to select.

In my app, I basically have a UISegmentedControl embedded in a scrollview. The UISegmentedControl is too long for the screen to display all of the values, so I created a scrollview that is the width of the screen, whose content width is the length of the UISegmentedControl, and to access the non-visible segments, the user swipes the "scrollview" and the segmented control slides.

This worked perfectly up until iOS 13, and now, the user can't scroll the horizontal background scrollview while dragging on the segmented control because I am assuming the scrollview scroll recognizer is overridden by the new scrollview swipe to select gesture.

I have tried even removing ALL gesture recognizers for the UISegmentedControl and all of its subviews recursively, and the swipe to change selection gesture still works... I am stuck.

Thanks, let me know if the problem is unclear

Author:ellek,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58177165/ios-13-segmented-control-remove-swipe-gesture-to-select-segment
Aystub :

I have a similar setup (UISegmentedControl inside a UIScrollView bc it's too long and the client didn't want to compress the content to fit). This worked for me (Built on Xcode 11.1):\n\nclass NoSwipeSegmentedControl: UISegmentedControl {\n\n override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {\n return true\n }\n}\n\n\nThen set the class of my UISegmentedControl to that. In my app this only prevents the swipe-to-select gesture on UISegmentedControl objects embedded within a UIScrollView. If it is not in a UIScrollView nothing behaves any differently. Which makes sense because gestureRecognizerShouldBegin() returns true by default. So why this allows the UIScrollView to take priority on the swipe gesture, I have no idea. But hope it helps.",
2019-10-02T00:52:40
mazend :

I upgraded @Aystub's answer. You can only allow UITapGestureRecogniger to select a segment.\n\nclass NoSwipeSegmentedControl: UISegmentedControl {\n override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {\n\n if(gestureRecognizer.isKind(of: UITapGestureRecognizer.self)){\n return false\n }else{\n return true\n }\n\n }\n}\n",
2020-04-13T14:58:37
yy