Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -530,9 +531,11 @@ type ScrollViewBaseProps = Readonly<{
* applied slightly differently than with a regular `<View>`:
*
* - 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTAssert.h>
#import <React/RCTConstants.h>
#import <React/RCTScrollEvent.h>
#import <React/RCTUtils.h>

#import <react/featureflags/ReactNativeFeatureFlags.h>
#import <react/renderer/components/scrollview/RCTComponentViewHelpers.h>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
{
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading