You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[iOS] react-native-screens v4.19.0 breaks the global back swipe gesture on pager views #1050
Any react-native-pager-view version (tested 6.8.0, 6.8.1, 8.0.0)
Description
I have filed the same issue with react-native-screens here: software-mansion/react-native-screens#3512
Since this is an issue with the interaction between these two libraries, I thought it was worth opening one here too.
After upgrading to react-native-screens v4.19.0, the global back gesture does not work if it's on top of a pager view. I would speculate it's due to software-mansion/react-native-screens#3420
Still reproduces on 8.0.2 + react-native-screens 4.26.2, iOS 26. This patch (for v8.0.2) fixes it.
diff --git a/ios/PagerGestureObserver.swift b/ios/PagerGestureObserver.swift
new file mode 100644
index 0000000000000000000000000000000000000000..9a5d38903e42af859a88abee26054725e34b2717
--- /dev/null+++ b/ios/PagerGestureObserver.swift@@ -0,0 +1,123 @@+import ObjectiveC+import UIKit++/**+ Keeps the pager's pan from swallowing the navigation controller's back gesture.++ On iOS 26 that gesture spans the whole screen instead of its edge, so a pager on+ top of it takes every horizontal drag (callstack/react-native-pager-view#1050).+ Allowing the two to run simultaneously does not help — UIKit still hands the+ touch to the scroll view — so the pan has to be refused before it begins. That+ answer belongs to `gestureRecognizerShouldBegin`, which the pan asks its+ delegate, and UIKit insists that delegate is the scroll view itself. Hence the+ runtime subclass, the same trick `disableSafeArea()` uses in `Extensions.swift`.++ Only the first page yields, and only while there is somewhere to go back to;+ there a back-swipe has nothing to scroll to anyway. Every other drag reaches the+ original implementation untouched.+ */+final class PagerGestureObserver: NSObject {+ var currentPage = 0+ var layoutDirection: PagerLayoutDirection = .ltr++ private weak var collectionView: UICollectionView?++ func attach(to collectionView: UICollectionView) {+ guard self.collectionView !== collectionView else { return }++ self.collectionView = collectionView+ objc_setAssociatedObject(+ collectionView,+ &PagerGestureObserver.associationKey,+ self,+ .OBJC_ASSOCIATION_RETAIN_NONATOMIC+ )+ PagerGestureObserver.installGestureArbitration(on: collectionView)+ }++ fileprivate func yieldsToBackGesture(in scrollView: UIScrollView) -> Bool {+ guard currentPage == 0, canNavigateBack(from: scrollView) else { return false }++ let velocity = scrollView.panGestureRecognizer.velocity(in: scrollView)++ return layoutDirection == .ltr ? velocity.x > 0 : velocity.x < 0+ }++ /// Walks the responder chain because the pod does not import React's `UIView`+ /// category, so `reactViewController()` is unavailable here.+ private func canNavigateBack(from view: UIView) -> Bool {+ var responder: UIResponder? = view.next++ while let current = responder {+ if let navigationController = current as? UINavigationController {+ return navigationController.viewControllers.count > 1+ }++ responder = current.next+ }++ return false+ }+}++private extension PagerGestureObserver {+ static var associationKey: UInt8 = 0+ static let subclassSuffix = "_PagerBackGesture"++ static func installGestureArbitration(on collectionView: UICollectionView) {+ guard let currentClass = object_getClass(collectionView) 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(collectionView, 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,+ gestureRecognizer === scrollView.panGestureRecognizer,+ 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(collectionView, subclass)+ }+}diff --git a/ios/PagerView.swift b/ios/PagerView.swift
index 7b296a0ecef30ea331edf97573270c6f9d1541ea..0e800a0d9747c1c9c956782c38c6a799552a559c 100644
--- a/ios/PagerView.swift+++ b/ios/PagerView.swift@@ -4,6 +4,7 @@ import SwiftUI
struct PagerView: View {
@ObservedObject var props: PagerViewProps
@State private var scrollDelegate = PagerScrollDelegate()
+ @State private var gestureObserver = PagerGestureObserver()
weak var delegate: PagerViewProviderDelegate?
@Weak var collectionView: UICollectionView?
@@ -39,6 +40,10 @@ struct PagerView: View {
scrollDelegate.orientation = props.orientation
collectionView.delegate = scrollDelegate
}
++ gestureObserver.attach(to: collectionView)+ gestureObserver.currentPage = props.currentPage+ gestureObserver.layoutDirection = props.layoutDirection
}
.onAppear {
// Apply initial prop values that .onChange won't catch
@@ -54,6 +59,7 @@ struct PagerView: View {
}
}
.onChange(of: props.currentPage) { newValue in
+ gestureObserver.currentPage = newValue
delegate?.onPageSelected(position: newValue)
}
.onChange(of: props.scrollEnabled) { newValue in
Activity
mozzius commentedon Jan 8, 2026
Dug into it a bit, and it needs to respond to the new iOS26 system back gesture. This patch (for v6.8.0) fixes it
mozzius commentedon Jan 8, 2026
cc @okwasniewski - I think the same issue affects v8.0.0
nickbvr commentedon Aug 12, 2026
Still reproduces on 8.0.2 +
react-native-screens4.26.2, iOS 26. This patch (for v8.0.2) fixes it.fix(ios): yield to the navigation back gesture on the first page
fix(ios): yield to the navigation back gesture on the first page (cal…