Repository navigation
fix(ios): yield to the navigation back gesture on the first page #1195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abulenok
wants to merge
3
commits into
callstack:main
Choose a base branch
from
abulenok:fix/ios-yield-to-back-gesture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import ObjectiveC | ||
| import UIKit | ||
|
|
||
| /** | ||
| Keeps the pager's pan from swallowing the navigation controller's back gesture | ||
| (callstack/react-native-pager-view#1050). UIKit hands the touch to the scroll | ||
| view even when recognizing simultaneously, so the pan must be refused in | ||
| `gestureRecognizerShouldBegin`, whose delegate has to be the scroll view | ||
| itself. Hence the runtime subclass, as in `disableSafeArea()`. | ||
|
|
||
| Only an outward drag on the first page of a horizontal pager yields, and only | ||
| while a back recognizer is actually tracking the touch. A deeper stack alone is | ||
| not enough: the pop recognizers may be disabled, refused by their delegate, or | ||
| edge-only with the drag starting mid-screen, and yielding would just kill the | ||
| overdrag bounce. | ||
|
|
||
| iOS 26's private `UIScrollViewPagingSwipeGestureRecognizer` flips pages on a | ||
| flick without the pan and asks the same method, so it is refused too. | ||
| */ | ||
| final class PagerGestureObserver: NSObject { | ||
| var currentPage = 0 | ||
| var layoutDirection: PagerLayoutDirection = .ltr | ||
| var orientation: UICollectionView.ScrollDirection = .horizontal | ||
|
|
||
| /// Attaches to the scroll view that pages: the collection view on iOS 16+, | ||
| /// the scroll views it embeds on iOS 15 (see `PagerViewIOS15Compat`). | ||
| func attach(to scrollView: UIScrollView) { | ||
| let attached = objc_getAssociatedObject(scrollView, &PagerGestureObserver.associationKey) | ||
| guard attached as? PagerGestureObserver !== self else { return } | ||
|
|
||
| objc_setAssociatedObject( | ||
| scrollView, | ||
| &PagerGestureObserver.associationKey, | ||
| self, | ||
| .OBJC_ASSOCIATION_RETAIN_NONATOMIC | ||
| ) | ||
| PagerGestureObserver.installGestureArbitration(on: scrollView) | ||
| } | ||
|
|
||
| fileprivate func yieldsToBackGesture(in scrollView: UIScrollView) -> Bool { | ||
| guard orientation == .horizontal, currentPage == 0, backGestureCanHandleTouch(in: scrollView) else { | ||
| return false | ||
| } | ||
|
|
||
| // The pan keeps tracking the touch even after it was refused, so it still | ||
| // tells the direction when the paging swipe recognizer asks later in the | ||
| // same touch sequence. Fall back to the translation when the velocity has | ||
| // not been sampled yet. | ||
| let pan = scrollView.panGestureRecognizer | ||
| var dx = pan.velocity(in: scrollView).x | ||
| if dx == 0 { | ||
| dx = pan.translation(in: scrollView).x | ||
| } | ||
|
|
||
| return layoutDirection == .ltr ? dx > 0 : dx < 0 | ||
| } | ||
|
|
||
| fileprivate static func isPagingRecognizer( | ||
| _ gestureRecognizer: UIGestureRecognizer, | ||
| of scrollView: UIScrollView | ||
| ) -> Bool { | ||
| if gestureRecognizer === scrollView.panGestureRecognizer { | ||
| return true | ||
| } | ||
| return NSStringFromClass(type(of: gestureRecognizer)) == pagingSwipeRecognizerClassName | ||
| } | ||
|
|
||
| /// Walks the responder chain because the pod does not import React's `UIView` | ||
| /// category, so `reactViewController()` is unavailable here. | ||
| private func backGestureCanHandleTouch(in scrollView: UIScrollView) -> Bool { | ||
| var responder: UIResponder? = scrollView.next | ||
| var navigationController: UINavigationController? | ||
|
|
||
| while let current = responder { | ||
| if let found = current as? UINavigationController { | ||
| navigationController = found | ||
| break | ||
| } | ||
|
|
||
| responder = current.next | ||
| } | ||
|
|
||
| guard let navigationController, navigationController.viewControllers.count > 1 else { | ||
| return false | ||
| } | ||
|
|
||
| var candidates: [UIGestureRecognizer] = [] | ||
|
|
||
| if let recognizer = navigationController.interactivePopGestureRecognizer { | ||
| candidates.append(recognizer) | ||
| } | ||
|
|
||
| if #available(iOS 26.0, *), | ||
| let recognizer = navigationController.interactiveContentPopGestureRecognizer | ||
| { | ||
| candidates.append(recognizer) | ||
| } | ||
|
|
||
| // react-native-screens installs its own swipe recognizers on the stack view | ||
| // above the navigation controller's view. | ||
| var ancestor = scrollView.superview | ||
| while let view = ancestor { | ||
| for recognizer in view.gestureRecognizers ?? [] | ||
| where Self.screensBackRecognizerClassNames.contains(NSStringFromClass(type(of: recognizer))) { | ||
| candidates.append(recognizer) | ||
| } | ||
| ancestor = view.superview | ||
| } | ||
|
|
||
| return candidates.contains(where: Self.isTrackingTouch) | ||
| } | ||
|
|
||
| /// A recognizer whose delegate refused the touch never receives it, and an | ||
| /// edge recognizer fails once the touch starts away from its edge, so only | ||
| /// one still holding touches in a live state can take over the drag. | ||
| private static func isTrackingTouch(_ recognizer: UIGestureRecognizer) -> Bool { | ||
| guard recognizer.isEnabled, recognizer.numberOfTouches > 0 else { return false } | ||
|
|
||
| switch recognizer.state { | ||
| case .possible, .began, .changed: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private extension PagerGestureObserver { | ||
| static var associationKey: UInt8 = 0 | ||
| static let subclassSuffix = "_PagerBackGesture" | ||
| static let pagingSwipeRecognizerClassName = "UIScrollViewPagingSwipeGestureRecognizer" | ||
| static let screensBackRecognizerClassNames: Set<String> = [ | ||
| "RNSPanGestureRecognizer", | ||
| "RNSScreenEdgeGestureRecognizer", | ||
| ] | ||
|
|
||
| static func installGestureArbitration(on scrollView: UIScrollView) { | ||
| guard let currentClass = object_getClass(scrollView) else { return } | ||
|
|
||
| let currentName = String(cString: class_getName(currentClass)) | ||
|
|
||
| // Already wearing the subclass, e.g. after a recycle. | ||
| guard !currentName.hasSuffix(subclassSuffix) else { return } | ||
|
|
||
| let subclassName = currentName.appending(subclassSuffix) | ||
|
|
||
| if let existing = NSClassFromString(subclassName) { | ||
| object_setClass(scrollView, existing) | ||
| return | ||
| } | ||
|
|
||
| let selector = #selector(UIGestureRecognizerDelegate.gestureRecognizerShouldBegin(_:)) | ||
|
|
||
| guard | ||
| let subclassNameUtf8 = (subclassName as NSString).utf8String, | ||
| let subclass = objc_allocateClassPair(currentClass, subclassNameUtf8, 0), | ||
| let method = class_getInstanceMethod(currentClass, selector) | ||
| else { return } | ||
|
|
||
| typealias Implementation = @convention(c) (AnyObject, Selector, UIGestureRecognizer) -> Bool | ||
|
|
||
| let superImplementation = unsafeBitCast( | ||
| method_getImplementation(method), | ||
| to: Implementation.self | ||
| ) | ||
|
|
||
| let override: @convention(block) (AnyObject, UIGestureRecognizer) -> Bool = { | ||
| object, gestureRecognizer in | ||
| if | ||
| let scrollView = object as? UIScrollView, | ||
| PagerGestureObserver.isPagingRecognizer(gestureRecognizer, of: scrollView), | ||
| let observer = objc_getAssociatedObject( | ||
| scrollView, &PagerGestureObserver.associationKey | ||
| ) as? PagerGestureObserver, | ||
| observer.yieldsToBackGesture(in: scrollView) | ||
| { | ||
| return false | ||
| } | ||
|
|
||
| return superImplementation(object, selector, gestureRecognizer) | ||
| } | ||
|
|
||
| class_addMethod( | ||
| subclass, | ||
| selector, | ||
| imp_implementationWithBlock(override), | ||
| method_getTypeEncoding(method) | ||
| ) | ||
|
|
||
| objc_registerClassPair(subclass) | ||
| object_setClass(scrollView, subclass) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stops at the nearest
UINavigationControllerand returnsfalsewhen it has one view controller. A pager on the root screen of a nested stack therefore never yields, even when the outer stack can pop (common with nested navigators in Expo Router and react-navigation). Skipping stacks that can't pop fixes it: