Skip to content

Commit 06cc264

Browse files
committed
Fix ScrollView contentInset area not responding to scroll gestures on iOS
Implements a runtime swizzling fix for React Native 0.81+ bug where ScrollView's contentInset area fails to respond to touch events. This prevents users from initiating scroll gestures by touching the inset padding area, which is critical for chat UX patterns where contentInset positions messages at the top of the screen. ## Problem React Native's RCTScrollViewComponentView.betterHitTest returns the container view (self) instead of the underlying UIScrollView instance when the touch is in the contentInset area. This causes touch events to be intercepted by the container, preventing the scroll view from receiving gestures. Upstream issue: facebook/react-native#54123 ## Solution Uses Objective-C runtime method swizzling to override hitTest:withEvent: on the ScrollView's container view. The fix mirrors the upstream patch: - Calls the original hitTest:withEvent: implementation - When the result is self (the container) - which is the bug - dynamically finds and returns the UIScrollView child instead - Otherwise preserves the original result (subviews, nil, etc.) Key implementation detail: the UIScrollView is looked up dynamically at hit-test time rather than captured during swizzling. This ensures the fix works across React Native hot reloads and full refreshes where the view hierarchy is recreated but the swizzled class persists. Implementation follows the same pattern as PR kirillzyusko#1336's scrollRectToVisible fix: dynamic subclass creation with idempotent prefix check, applied lazily in didMoveToWindow. ## Benefits ✅ Users can now scroll by touching contentInset area ✅ Interactive content (Pressables, buttons, inputs) continues to work normally ✅ Survives React Native hot reloads and full refreshes ✅ Fixes chat UX where messages need top positioning via contentInset ✅ Non-breaking: defensive implementation returns early if structure unexpected ✅ Compatible with existing scrollRectToVisible noop fix ✅ Zero-cost for apps not using ClippingScrollView ✅ Provides immediate relief while waiting for upstream React Native fix ## Performance Considerations The dynamic scrollView lookup only executes when: - The original hitTest returns the container (empty space/contentInset area) - NOT when touching actual content (messages, buttons, etc.) Cost: iterating through container's direct subviews (typically 1-3 views) - UIScrollView + maybe scroll indicators - Just pointer comparisons and class checks - Negligible performance impact ## Risks and Considerations ⚠️ Runtime swizzling: Fragile if React Native's view hierarchy changes Mitigation: Defensive checks, silently returns if container not found ⚠️ Dynamic lookup on every hit test in contentInset area: Small overhead Mitigation: Only 1-3 subviews to check, only runs for empty space touches ⚠️ React Native version variations: Different RN versions may have different hierarchies Mitigation: Graceful degradation - no fix applied but doesn't break ⚠️ Calling original implementation: Assumes it's safe to invoke Mitigation: Standard ObjC pattern, same lifetime guarantees as the view ## Testing Recommendations - Test contentInset with top/bottom/left/right values - Verify touch in contentInset area initiates scroll - Verify interactive elements (Pressables, buttons, inputs) remain functional - Test with inverted ScrollViews (chat pattern) - Verify behavior persists after hot reload - Verify behavior persists after full refresh - Test keyboard interactions remain functional - Verify no console warnings - Test on iOS 17+ with React Native 0.81+ ## Technical Details Implementation in ClippingScrollViewDecoratorViewManager.mm: - Added KCApplyFixedHitTest() function that swizzles hitTest:withEvent: - Modified didMoveToWindow() to apply both fixes - Captures original IMP and calls it via function pointer - Conditional: if (result == self) lookup and return UIScrollView child - Dynamic lookup avoids stale references across RN refreshes - Uses KC_FixedHitTest_ prefix for idempotent check
1 parent 4910c3d commit 06cc264

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3019,4 +3019,4 @@ SPEC CHECKSUMS:
30193019

30203020
PODFILE CHECKSUM: c51a5e2124b01c5794c41f1f13ee16704466637a
30213021

3022-
COCOAPODS: 1.15.2
3022+
COCOAPODS: 1.16.2

ios/views/ClippingScrollViewDecoratorViewManager.mm

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,71 @@ static void KCApplyNoopScrollRectToVisible(UIScrollView *scrollView)
8383
object_setClass(scrollView, subclass);
8484
}
8585

86+
static void KCApplyFixedHitTest(UIScrollView *scrollView)
87+
{
88+
if (!scrollView || !scrollView.superview) {
89+
return;
90+
}
91+
92+
UIView *container = scrollView.superview;
93+
Class originalClass = object_getClass(container);
94+
NSString *originalClassName = NSStringFromClass(originalClass);
95+
96+
// Already patched — nothing to do
97+
if ([originalClassName hasPrefix:@"KC_FixedHitTest_"]) {
98+
return;
99+
}
100+
101+
NSString *subclassName = [@"KC_FixedHitTest_" stringByAppendingString:originalClassName];
102+
Class subclass = NSClassFromString(subclassName);
103+
104+
if (!subclass) {
105+
subclass = objc_allocateClassPair(originalClass, subclassName.UTF8String, 0);
106+
if (!subclass) {
107+
return;
108+
}
109+
110+
Method original = class_getInstanceMethod(originalClass, @selector(hitTest:withEvent:));
111+
if (original) {
112+
// Get the original implementation to call it
113+
IMP originalImp = method_getImplementation(original);
114+
UIView *(*originalHitTest)(id, SEL, CGPoint, UIEvent *) =
115+
(UIView *(*)(id, SEL, CGPoint, UIEvent *))originalImp;
116+
117+
IMP fixedHitTestImp = imp_implementationWithBlock(
118+
^UIView *(__unsafe_unretained UIView *self, CGPoint point, UIEvent *event) {
119+
// Call the original implementation
120+
UIView *result = originalHitTest(self, @selector(hitTest:withEvent:), point, event);
121+
122+
// This is the fix: when RN's betterHitTest returns self (the container),
123+
// return the scrollView instead so touches can reach it
124+
if (result == self) {
125+
// Dynamically find the scrollView at hit-test time to handle RN refreshes
126+
// where the scrollView instance is recreated but the swizzled class persists
127+
for (UIView *subview in self.subviews) {
128+
if ([subview isKindOfClass:[UIScrollView class]] &&
129+
![subview isKindOfClass:[UITextView class]]) {
130+
return subview;
131+
}
132+
}
133+
}
134+
135+
return result;
136+
});
137+
138+
class_addMethod(
139+
subclass,
140+
@selector(hitTest:withEvent:),
141+
fixedHitTestImp,
142+
method_getTypeEncoding(original));
143+
}
144+
145+
objc_registerClassPair(subclass);
146+
}
147+
148+
object_setClass(container, subclass);
149+
}
150+
86151
#pragma mark - Manager
87152

88153
@implementation ClippingScrollViewDecoratorViewManager
@@ -133,6 +198,7 @@ - (void)didMoveToWindow
133198
if (self.window) {
134199
UIScrollView *scrollView = KCFindFirstScrollView(self);
135200
KCApplyNoopScrollRectToVisible(scrollView);
201+
KCApplyFixedHitTest(scrollView);
136202
}
137203
}
138204

0 commit comments

Comments
 (0)