From 0f70674280a3ba3b3e6ce27ff7128e0df21ee6de Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 16 Nov 2026 02:46:16 -0800 Subject: [PATCH] Preserve native ScrollView insets during safe-area updates Summary: Replace only the previous safe-area correction when the adjusted content inset changes, so insets added by native refresh controls and keyboard handling are not overwritten. Explicit base-content-inset updates still replace the requested inset, and changing the scroll axis or disabling content padding removes the previous correction. Changelog: [iOS][Fixed] - Preserve native refresh-control and keyboard insets during ScrollView safe-area updates Differential Revision: D132237950 --- .../ScrollView/RCTEnhancedScrollView.mm | 43 ++--- .../RCTScrollViewComponentViewTests.mm | 157 ++++++++++++++++++ 2 files changed, 181 insertions(+), 19 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTEnhancedScrollView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTEnhancedScrollView.mm index 9d116bdd74de..926b62ecba8c 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTEnhancedScrollView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTEnhancedScrollView.mm @@ -15,6 +15,7 @@ @interface RCTEnhancedScrollView () @implementation RCTEnhancedScrollView { __weak id _publicDelegate; BOOL _isSetContentOffsetDisabled; + UIEdgeInsets _safeAreaContentInsetAdjustment; } + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key @@ -117,18 +118,14 @@ - (void)centerContentIfNeeded - (void)setBaseContentInset:(UIEdgeInsets)baseContentInset { _baseContentInset = baseContentInset; - if (_padsContentForSafeArea) { - [self applyBaseContentInset]; - } else { - self.contentInset = baseContentInset; - } + [self applyContentInsetPreservingExternalInsets:NO]; } - (void)setHorizontal:(BOOL)horizontal { _horizontal = horizontal; if (_padsContentForSafeArea) { - [self applyBaseContentInset]; + [self applyContentInsetPreservingExternalInsets:YES]; } } @@ -138,37 +135,45 @@ - (void)setPadsContentForSafeArea:(BOOL)padsContentForSafeArea return; } _padsContentForSafeArea = padsContentForSafeArea; - [self applyBaseContentInset]; + [self applyContentInsetPreservingExternalInsets:YES]; } - (void)adjustedContentInsetDidChange { [super adjustedContentInsetDidChange]; if (_padsContentForSafeArea) { - [self applyBaseContentInset]; + [self applyContentInsetPreservingExternalInsets:YES]; } } /* - * Applies `baseContentInset`, less what UIKit adds across the scrollable axis for the safe area while the content is - * padded for it. `ScrollView` lays the content out clear of the system UI rather than scrolling it under; the - * content inset would apply the safe area a second time. + * Cancels UIKit's safe area inset across the scrollable axis while the content is padded for it. Only this + * correction is replaced; native controls can independently adjust the content inset. */ -- (void)applyBaseContentInset +- (void)applyContentInsetPreservingExternalInsets:(BOOL)preserveExternalInsets { - UIEdgeInsets contentInset = _baseContentInset; + UIEdgeInsets current = self.contentInset; + UIEdgeInsets adjustment = UIEdgeInsetsZero; if (_padsContentForSafeArea) { UIEdgeInsets adjusted = self.adjustedContentInset; - UIEdgeInsets current = self.contentInset; if (_horizontal) { - contentInset.top -= adjusted.top - current.top; - contentInset.bottom -= adjusted.bottom - current.bottom; + adjustment.top = adjusted.top - current.top; + adjustment.bottom = adjusted.bottom - current.bottom; } else { - contentInset.left -= adjusted.left - current.left; - contentInset.right -= adjusted.right - current.right; + adjustment.left = adjusted.left - current.left; + adjustment.right = adjusted.right - current.right; } } - if (UIEdgeInsetsEqualToEdgeInsets(contentInset, self.contentInset)) { + UIEdgeInsets inset = preserveExternalInsets ? current : _baseContentInset; + UIEdgeInsets previousAdjustment = preserveExternalInsets ? _safeAreaContentInsetAdjustment : UIEdgeInsetsZero; + UIEdgeInsets contentInset = UIEdgeInsetsMake( + inset.top + (previousAdjustment.top - adjustment.top), + inset.left + (previousAdjustment.left - adjustment.left), + inset.bottom + (previousAdjustment.bottom - adjustment.bottom), + inset.right + (previousAdjustment.right - adjustment.right)); + // Updating contentInset can synchronously call adjustedContentInsetDidChange. + _safeAreaContentInsetAdjustment = adjustment; + if (UIEdgeInsetsEqualToEdgeInsets(contentInset, current)) { return; } // Content pinned to the leading edge across the scrollable axis stays pinned, as UIKit keeps it when it changes the diff --git a/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm b/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm index 82b350755668..cafe2379512e 100644 --- a/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +#import #import #import #import @@ -27,6 +28,38 @@ @interface RCTScrollViewComponentView (Tests) - (void)_keyboardWillChangeFrame:(NSNotification *)notification; @end +@interface RCTInsetTestScrollView : RCTEnhancedScrollView +@property (nonatomic, assign) UIEdgeInsets simulatedSafeAreaInsets; +@property (nonatomic, assign) BOOL notifiesInsetChangesSynchronously; +@property (nonatomic, assign) BOOL keepsContentOffsetOnInsetChange; +@end + +@implementation RCTInsetTestScrollView + +- (void)setContentInset:(UIEdgeInsets)contentInset +{ + CGPoint offset = self.contentOffset; + [super setContentInset:contentInset]; + if (_keepsContentOffsetOnInsetChange) { + self.contentOffset = offset; + } + if (_notifiesInsetChangesSynchronously) { + [self adjustedContentInsetDidChange]; + } +} + +- (UIEdgeInsets)adjustedContentInset +{ + UIEdgeInsets inset = self.contentInset; + inset.top += _simulatedSafeAreaInsets.top; + inset.left += _simulatedSafeAreaInsets.left; + inset.bottom += _simulatedSafeAreaInsets.bottom; + inset.right += _simulatedSafeAreaInsets.right; + return inset; +} + +@end + @interface RCTScrollViewComponentViewTests : XCTestCase @end @@ -61,6 +94,130 @@ - (void)testAutomaticallyAdjustKeyboardInsetsAcrossRecycling XCTAssertEqual(view.scrollView.contentInset.bottom, 50); } +// XCTest assertion macros use variadic reporting and catch framework exceptions by value. +// NOLINTBEGIN(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference) +- (void)testSafeAreaCorrectionPreservesPullToRefreshInset +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.padsContentForSafeArea = YES; + view.contentInset = UIEdgeInsetsMake(60, 0, 0, 0); + + [view adjustedContentInsetDidChange]; + view.horizontal = NO; + [view adjustedContentInsetDidChange]; + XCTAssertEqual(view.contentInset.top, 60); + + UIEdgeInsets inset = view.contentInset; + inset.top -= 60; + view.contentInset = inset; + [view adjustedContentInsetDidChange]; + XCTAssertEqual(view.contentInset.top, 0); +} + +- (void)testSafeAreaCorrectionPreservesKeyboardInset +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40); + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25); + view.padsContentForSafeArea = YES; + UIEdgeInsets inset = view.contentInset; + inset.bottom += 300; + view.contentInset = inset; + + [view adjustedContentInsetDidChange]; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 5, 330, 15))); + [view adjustedContentInsetDidChange]; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 5, 330, 15))); +} + +- (void)testSafeAreaChangesPreserveExternalInsets +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40); + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25); + view.padsContentForSafeArea = YES; + UIEdgeInsets inset = view.contentInset; + inset.top += 60; + inset.left += 7; + view.contentInset = inset; + + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 25, 0, 35); + [view adjustedContentInsetDidChange]; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(70, 2, 30, 5))); + + view.padsContentForSafeArea = NO; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(70, 27, 30, 40))); +} + +- (void)testHorizontalSafeAreaCorrectionPreservesExternalInsets +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.horizontal = YES; + view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40); + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(5, 0, 15, 0); + view.padsContentForSafeArea = YES; + UIEdgeInsets inset = view.contentInset; + inset.left += 60; + view.contentInset = inset; + + [view adjustedContentInsetDidChange]; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(5, 80, 15, 40))); + + view.horizontal = NO; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 80, 30, 40))); +} + +- (void)testBaseInsetReplacementKeepsContentPinnedAcrossTheScrollAxis +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.keepsContentOffsetOnInsetChange = YES; + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25); + view.padsContentForSafeArea = YES; + view.contentOffset = CGPointZero; + + view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40); + XCTAssertEqual(view.contentOffset.x, -20); + + view.contentOffset = CGPointMake(100, 0); + view.baseContentInset = UIEdgeInsetsMake(20, 30, 40, 50); + XCTAssertEqual(view.contentOffset.x, 100); +} + +- (void)testSynchronousInsetCallbacksPreserveFractionalInsets +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.notifiesInsetChangesSynchronously = YES; + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15.5, 0, 25.25); + view.padsContentForSafeArea = YES; + view.contentInset = UIEdgeInsetsMake(60.5, -15.5, 0, -25.25); + + [view adjustedContentInsetDidChange]; + [view adjustedContentInsetDidChange]; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(60.5, -15.5, 0, -25.25))); + + view.baseContentInset = UIEdgeInsetsZero; + view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; + view.padsContentForSafeArea = NO; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsZero)); + + view.baseContentInset = UIEdgeInsetsMake(1.25, 2.5, 3.75, 4.5); + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(1.25, 2.5, 3.75, 4.5))); +} + +- (void)testBaseContentInsetReplacesPreviousInsetWithoutAccumulatingSafeAreaCorrection +{ + RCTInsetTestScrollView *view = [RCTInsetTestScrollView new]; + view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25); + view.padsContentForSafeArea = YES; + view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40); + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 5, 30, 15))); + + view.baseContentInset = UIEdgeInsetsMake(20, 30, 40, 50); + [view adjustedContentInsetDidChange]; + XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(20, 15, 40, 25))); +} +// NOLINTEND(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference) + @end #endif