From 5eb935d5b78958c3912e359556fe178ff2b2414f Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Thu, 19 Nov 2026 19:46:21 +0000 Subject: [PATCH] [ScrollView][iOS] Fix contentContainerStyle padding absorbed under navigation and tab bars --- .../Components/ScrollView/ScrollView.js | 15 ++++--- .../RCTScrollContentViewComponentView.h | 5 ++- .../ScrollView/RCTScrollViewComponentView.mm | 43 +++++++++++++++---- .../scrollview/ScrollContentViewState.h | 7 +-- .../tests/ScrollContentViewTest.cpp | 13 ++++++ 5 files changed, 64 insertions(+), 19 deletions(-) diff --git a/packages/react-native/Libraries/Components/ScrollView/ScrollView.js b/packages/react-native/Libraries/Components/ScrollView/ScrollView.js index a9445922d19f..8c5827341cff 100644 --- a/packages/react-native/Libraries/Components/ScrollView/ScrollView.js +++ b/packages/react-native/Libraries/Components/ScrollView/ScrollView.js @@ -412,9 +412,10 @@ export type ScrollViewPropsIOS = Readonly<{ * false. * * Note: With any value other than `'never'`, point-valued - * `contentContainerStyle` padding on edges where an inset applies is - * absorbed by the inset, instead of adding to it. See - * `contentContainerStyle`. + * `contentContainerStyle` padding on an edge with a safe area inset counts + * toward the inset, instead of adding to it. Along the scrollable axis, the + * padding still adds to an inset that includes a navigation bar, tab bar or + * toolbar. See `contentContainerStyle`. * * @default `'never'` * @platform ios @@ -530,9 +531,11 @@ type ScrollViewBaseProps = Readonly<{ * applied slightly differently than with a regular ``: * * - Point values: If a system safe area inset applies on a given edge, the - * **greater of** the `padding` or inset length will be applied. An edge - * without an inset, such as the top of a ScrollView below a header, keeps - * its full `padding`. + * **greater of** the `padding` or inset length will be applied. Along the + * scrollable axis, an inset that includes a navigation bar, tab bar or + * toolbar is the exception: the `padding` adds to it, keeping its spacing + * from the bar. An edge without an inset, such as the top of a ScrollView + * below a custom header, keeps its full `padding`. * - Percentage values: Across the scrollable axis, used directly, overriding * any safe area padding. Along it, the safe area inset still applies, and * the `padding` adds to it. diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.h b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.h index 775dc107a355..2be6c1ed02a1 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.h +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.h @@ -24,8 +24,9 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, readonly) UIEdgeInsets safeAreaPadding; /* - * The content inset UIKit adds for the safe area along the scrollable axis, which the content container's own padding - * on those edges counts toward. The enclosing scroll view sets the pair of insets along its scrollable axis. + * The content inset UIKit adds along the scrollable axis on each edge where the window's safe area alone makes it up, + * which the content container's own padding on that edge counts toward. Zero on an edge where a navigation bar, tab bar + * or toolbar adds to the inset, so the padding adds on top. */ @property (nonatomic, readonly) UIEdgeInsets contentInsetAdjustment; 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 86d80594f71c..afb13251988c 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -10,6 +10,7 @@ #import #import #import +#import #import #import @@ -94,6 +95,27 @@ static BOOL RCTPadsContentForSafeArea(ContentInsetAdjustmentBehavior contentInse contentInsetAdjustmentBehavior == ContentInsetAdjustmentBehavior::Always; } +static UIEdgeInsets RCTWindowSafeAreaInsetsInView(UIView *view) +{ + UIWindow *window = view.window; + if (window == nil) { + return UIEdgeInsetsZero; + } + UIEdgeInsets insets = window.safeAreaInsets; + CGRect frame = [view convertRect:view.bounds toView:nil]; + CGRect bounds = window.bounds; + return UIEdgeInsetsMake( + MAX(0, insets.top - CGRectGetMinY(frame)), + MAX(0, insets.left - CGRectGetMinX(frame)), + MAX(0, insets.bottom - (CGRectGetMaxY(bounds) - CGRectGetMaxY(frame))), + MAX(0, insets.right - (CGRectGetMaxX(bounds) - CGRectGetMaxX(frame)))); +} + +static CGFloat RCTWindowSafeAreaContentInset(CGFloat contentInset, CGFloat windowSafeAreaInset) +{ + return RCTRoundPixelValue(contentInset) > RCTRoundPixelValue(windowSafeAreaInset) ? 0 : contentInset; +} + @interface RCTScrollViewComponentView () < UIScrollViewDelegate, RCTScrollViewProtocol, @@ -904,10 +926,14 @@ - (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView } /* - * Hands the content container the safe area to lay its content out clear of: along the scrollable axis, the content - * inset UIKit adds for the safe area, which point-valued padding counts toward, unless - * `contentInsetAdjustmentBehavior` is `never`; and across the axis, the pair of insets it pads by, for `automatic` - * and `always`. Content still scrolls under the system UI along the axis. + * Hands the content container the safe area to lay its content out clear of. + * + * Along the scrollable axis, UIKit adds the safe area as a content inset unless `contentInsetAdjustmentBehavior` is + * `never`. On an edge where the window's safe area (the status bar, sensor housing or home indicator) alone makes up + * that inset, the content container's padding counts toward it. On an edge where a view controller's navigation bar, + * tab bar or toolbar adds to it, the padding adds on top, keeping the content's spacing from that bar. + * + * Across the axis, for `automatic` and `always`, the content container is padded by the whole safe area. */ - (void)_updateContentViewSafeAreaPadding { @@ -921,12 +947,13 @@ - (void)_updateContentViewSafeAreaPadding if (props.contentInsetAdjustmentBehavior != ContentInsetAdjustmentBehavior::Never) { UIEdgeInsets adjusted = scrollView.adjustedContentInset; UIEdgeInsets current = scrollView.contentInset; + UIEdgeInsets window = RCTWindowSafeAreaInsetsInView(scrollView); if (props.horizontal) { - contentInset.left = adjusted.left - current.left; - contentInset.right = adjusted.right - current.right; + contentInset.left = RCTWindowSafeAreaContentInset(adjusted.left - current.left, window.left); + contentInset.right = RCTWindowSafeAreaContentInset(adjusted.right - current.right, window.right); } else { - contentInset.top = adjusted.top - current.top; - contentInset.bottom = adjusted.bottom - current.bottom; + contentInset.top = RCTWindowSafeAreaContentInset(adjusted.top - current.top, window.top); + contentInset.bottom = RCTWindowSafeAreaContentInset(adjusted.bottom - current.bottom, window.bottom); } } if (scrollView.padsContentForSafeArea) { diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewState.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewState.h index 1dd8dac59175..9a20aaa1130b 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewState.h +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewState.h @@ -32,9 +32,10 @@ class ScrollContentViewState final { EdgeInsets safeAreaPadding{}; /* - * The content inset UIKit adds for the safe area along the scrollable axis, - * which the padding the style sets on those edges counts toward. The - * enclosing scroll view sets the pair of edges along its scrollable axis. + * The content inset UIKit adds along the scrollable axis on each edge where + * the window's safe area alone makes it up, which the padding the style sets + * on that edge counts toward. Zero on an edge where a navigation bar, tab + * bar or toolbar adds to the inset, so the padding adds on top. */ EdgeInsets contentInsetAdjustment{}; diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollContentViewTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollContentViewTest.cpp index 860b18967b58..b2ff5b6b4cc2 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollContentViewTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/tests/ScrollContentViewTest.cpp @@ -233,6 +233,19 @@ TEST(ScrollContentViewShadowNodeTest, keepsColumnPaddingBeyondTheContentInset) { EXPECT_EQ(style.padding(Edge::Bottom), StyleLength::points(66)); } +TEST(ScrollContentViewShadowNodeTest, keepsColumnPaddingBelowANavigationBar) { + yoga::Style style; + style.setPadding(Edge::Vertical, StyleLength::points(20)); + + ScrollContentViewShadowNode::padForSafeArea( + style, + stateWithContentInset({.left = 0, .top = 0, .right = 0, .bottom = 34}), + kLTR); + + EXPECT_EQ(style.padding(Edge::Top), StyleLength::points(20)); + EXPECT_EQ(style.padding(Edge::Bottom), StyleLength::points(0)); +} + TEST(ScrollContentViewShadowNodeTest, reducesRowPaddingByTheContentInsetInRTL) { yoga::Style style; style.setPadding(Edge::Horizontal, StyleLength::points(100));