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 1b02e8b2d39672..253b4c47642904 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; + CGPoint _foregroundContentOffset; } + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key @@ -97,6 +98,28 @@ - (void)setContentOffset:(CGPoint)contentOffset if (_isSetContentOffsetDisabled) { return; } + +#if TARGET_OS_IOS + UIWindowScene *scene = self.window.windowScene; + if (scene && scene.activationState == UISceneActivationStateBackground) { + if (!CGPointEqualToPoint(_foregroundContentOffset, CGPointZero)) { + BOOL isHorizontal = [self isHorizontal:self]; + CGSize viewportSize = self.bounds.size; + if (isHorizontal) { + contentOffset.x = + fmin(_foregroundContentOffset.x, + fmax(0, self.contentSize.width - viewportSize.width)); + } else { + contentOffset.y = + fmin(_foregroundContentOffset.y, + fmax(0, self.contentSize.height - viewportSize.height)); + } + } + } else { + _foregroundContentOffset = contentOffset; + } +#endif + super.contentOffset = CGPointMake( RCTSanitizeNaNValue(contentOffset.x, @"scrollView.contentOffset.x"), RCTSanitizeNaNValue(contentOffset.y, @"scrollView.contentOffset.y")); diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index 15e75f45632a60..cad5a55f669a1b 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -650,6 +650,13 @@ - (void)_updateStateWithContentOffset _avoidAdjustmentForMaintainVisibleContentPosition = enableImmediateUpdateModeForContentOffsetChanges; +#if TARGET_OS_IOS + UIWindowScene *scene = self.window.windowScene; + if (scene && scene.activationState == UISceneActivationStateBackground) { + return; + } +#endif + auto contentOffset = RCTPointFromCGPoint(_scrollView.contentOffset); BOOL isAccessibilityAPIUsed = _isAccessibilityAPIUsed; _state->updateState(