Skip to content

Commit 0845adf

Browse files
committed
[ScrollView][iOS] Change padding to use max of safe area inset/container value
1 parent b74247d commit 0845adf

13 files changed

Lines changed: 256 additions & 59 deletions

File tree

‎packages/react-native/Libraries/Components/ScrollView/ScrollView.js‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ export type ScrollViewPropsIOS = Readonly<{
392392
* scrolls under the system UI. Across it, every value except `'never'` pads
393393
* the content container by the insets, so content is laid out clear of the
394394
* system UI, such as the rail on iPhone Duo or camera cutout in landscape,
395-
* while the container's background still extends edge to edge.
395+
* while the container's background still extends edge to edge. On every
396+
* edge, the `contentContainerStyle` padding counts toward the inset, so
397+
* content sits at the larger of the two.
396398
*
397399
* @default `'never'`
398400
* @platform ios
@@ -482,8 +484,7 @@ type StickyHeaderComponentType = component(
482484

483485
type ScrollViewBaseProps = Readonly<{
484486
/**
485-
* These styles will be applied to the scroll view content container which
486-
* wraps all of the child views.
487+
* Styles applied to the content container which wraps all child views.
487488
*
488489
* Example:
489490
*
@@ -499,8 +500,25 @@ type ScrollViewBaseProps = Readonly<{
499500
* }
500501
* });
501502
* ```
503+
*
504+
* **Interaction with `contentInsetAdjustmentBehavior`**
505+
*
506+
* Commonly, `contentContainerStyle` is used to pad items away from the
507+
* screen edges or the system scrollbar.
508+
*
509+
* If setting `contentInsetAdjustmentBehavior` (recommended), `padding` is
510+
* applied slightly differently than with a regular `<View>`:
511+
*
512+
* - Pixel values: If a system safe area inset applies on a given edge, the
513+
* **greater of** the `padding` or inset length will be applied. An edge
514+
* without an inset, such as the top of a ScrollView below a header, keeps
515+
* its full `padding`.
516+
* - Percentage values: Across the scrollable axis, used directly, overriding
517+
* any safe area padding. Along it, the safe area inset still applies, and
518+
* the `padding` adds to it.
502519
*/
503520
contentContainerStyle?: ?ViewStyleProp,
521+
504522
/**
505523
* Used to manually set the starting scroll offset.
506524
*

‎packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ NS_ASSUME_NONNULL_BEGIN
1717
@interface RCTScrollContentViewComponentView : RCTViewComponentView
1818

1919
/*
20-
* The safe area the content is laid out clear of, as padding on top of the content container's own. The enclosing
20+
* The safe area the content is laid out clear of, as a floor on the content container's own padding. The enclosing
2121
* scroll view sets the pair of insets across its scrollable axis, and zero while its `contentInsetAdjustmentBehavior`
2222
* is `never`.
2323
*/
24-
@property (nonatomic, assign) UIEdgeInsets safeAreaPadding;
24+
@property (nonatomic, readonly) UIEdgeInsets safeAreaPadding;
25+
26+
/*
27+
* The content inset UIKit adds for the safe area along the scrollable axis, which the content container's own padding
28+
* on those edges counts toward. The enclosing scroll view sets the pair of insets along its scrollable axis.
29+
*/
30+
@property (nonatomic, readonly) UIEdgeInsets contentInsetAdjustment;
31+
32+
- (void)setSafeAreaPadding:(UIEdgeInsets)safeAreaPadding contentInsetAdjustment:(UIEdgeInsets)contentInsetAdjustment;
2533

2634
@end
2735

‎packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.mm‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,32 @@ - (instancetype)initWithFrame:(CGRect)frame
3030
return self;
3131
}
3232

33-
- (void)setSafeAreaPadding:(UIEdgeInsets)safeAreaPadding
33+
- (void)setSafeAreaPadding:(UIEdgeInsets)safeAreaPadding contentInsetAdjustment:(UIEdgeInsets)contentInsetAdjustment
3434
{
3535
_safeAreaPadding = safeAreaPadding;
36+
_contentInsetAdjustment = contentInsetAdjustment;
3637
[self _updateStateIfNecessary];
3738
}
3839

40+
static EdgeInsets RCTRoundedEdgeInsets(UIEdgeInsets insets)
41+
{
42+
return {
43+
.left = RCTRoundPixelValue(insets.left),
44+
.top = RCTRoundPixelValue(insets.top),
45+
.right = RCTRoundPixelValue(insets.right),
46+
.bottom = RCTRoundPixelValue(insets.bottom)};
47+
}
48+
3949
- (void)_updateStateIfNecessary
4050
{
4151
if (!_state) {
4252
return;
4353
}
4454

45-
UIEdgeInsets insets = _safeAreaPadding;
46-
insets.left = RCTRoundPixelValue(insets.left);
47-
insets.top = RCTRoundPixelValue(insets.top);
48-
insets.right = RCTRoundPixelValue(insets.right);
49-
insets.bottom = RCTRoundPixelValue(insets.bottom);
50-
5155
RCTI18nUtil *i18nUtil = [RCTI18nUtil sharedInstance];
5256
ScrollContentViewState newData;
53-
newData.safeAreaPadding = RCTEdgeInsetsFromUIEdgeInsets(insets);
57+
newData.safeAreaPadding = RCTRoundedEdgeInsets(_safeAreaPadding);
58+
newData.contentInsetAdjustment = RCTRoundedEdgeInsets(_contentInsetAdjustment);
5459
newData.swapLeftAndRightInRTL = i18nUtil.isRTL && i18nUtil.doLeftAndRightSwapInRTL;
5560
if (newData == _lastStateData) {
5661
return;
@@ -90,6 +95,7 @@ - (void)prepareForRecycle
9095
_state.reset();
9196
_lastStateData = {};
9297
_safeAreaPadding = UIEdgeInsetsZero;
98+
_contentInsetAdjustment = UIEdgeInsetsZero;
9399
}
94100

95101
@end

‎packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,16 @@ - (void)layoutSubviews
888888
[self _updateContentViewSafeAreaPadding];
889889
}
890890

891+
- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView
892+
{
893+
[self _updateContentViewSafeAreaPadding];
894+
}
895+
891896
/*
892-
* Hands the content container the safe area to pad its content by: the pair of insets across the scrollable axis,
893-
* unless `contentInsetAdjustmentBehavior` is `never`. Along the scrollable axis UIKit turns the safe area into content
894-
* insets instead, so content scrolls under the system UI.
897+
* Hands the content container the safe area to lay its content out clear of, unless `contentInsetAdjustmentBehavior`
898+
* is `never`: the pair of insets across the scrollable axis, which it pads by, and along the axis the content inset
899+
* UIKit adds for the safe area, which its own padding counts toward. Content still scrolls under the system UI along
900+
* the axis.
895901
*/
896902
- (void)_updateContentViewSafeAreaPadding
897903
{
@@ -900,17 +906,24 @@ - (void)_updateContentViewSafeAreaPadding
900906
}
901907
const auto &props = static_cast<const ScrollViewProps &>(*_props);
902908
UIEdgeInsets padding = UIEdgeInsetsZero;
909+
UIEdgeInsets contentInset = UIEdgeInsetsZero;
903910
if (props.contentInsetAdjustmentBehavior != ContentInsetAdjustmentBehavior::Never) {
904911
UIEdgeInsets insets = self.safeAreaInsets;
912+
UIEdgeInsets adjusted = _scrollView.adjustedContentInset;
913+
UIEdgeInsets current = _scrollView.contentInset;
905914
if (props.horizontal) {
906915
padding.top = insets.top;
907916
padding.bottom = insets.bottom;
917+
contentInset.left = adjusted.left - current.left;
918+
contentInset.right = adjusted.right - current.right;
908919
} else {
909920
padding.left = insets.left;
910921
padding.right = insets.right;
922+
contentInset.top = adjusted.top - current.top;
923+
contentInset.bottom = adjusted.bottom - current.bottom;
911924
}
912925
}
913-
((RCTScrollContentViewComponentView *)_contentView).safeAreaPadding = padding;
926+
[(RCTScrollContentViewComponentView *)_contentView setSafeAreaPadding:padding contentInsetAdjustment:contentInset];
914927
}
915928

916929
- (void)_handleScrollEndIfNeeded

‎packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewShadowNode.cpp‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "ScrollContentViewShadowNode.h"
99

10+
#include <algorithm>
1011
#include <array>
1112

1213
namespace facebook::react {
@@ -78,16 +79,21 @@ StyleLength resolvedPadding(
7879
}
7980

8081
/*
82+
* The safe area is a floor on the padding, as it is on UIKit's layout
83+
* margins: the edge takes the larger of its style padding and the inset.
84+
* Where UIKit already applies the inset as a content inset, the edge keeps
85+
* what its padding exceeds that by, which lands the content on the same floor.
8186
* A percentage padding keeps the edge as the style set it, with no inset
82-
* added, since Yoga holds one length per edge and a percentage has no point
83-
* value to add to.
87+
* applied, since Yoga holds one length per edge and a percentage has no point
88+
* value to compare.
8489
*/
85-
StyleLength padded(StyleLength value, Float inset) {
86-
if (inset == 0 || value.isPercent()) {
90+
StyleLength padded(StyleLength value, Float inset, Float contentInset) {
91+
if ((inset == 0 && contentInset == 0) || value.isPercent()) {
8792
return value;
8893
}
89-
auto base = value.isPoints() ? value.value().unwrap() : 0.0f;
90-
return StyleLength::points(base + inset);
94+
auto base = value.isPoints() ? Float{value.value().unwrap()} : Float{0};
95+
return StyleLength::points(
96+
std::max(std::max(base, inset) - contentInset, Float{0}));
9197
}
9298

9399
} // namespace
@@ -131,22 +137,31 @@ void ScrollContentViewShadowNode::applySafeAreaPadding() {
131137
const ScrollContentViewState& state,
132138
LayoutDirection layoutDirection) {
133139
const auto& insets = state.safeAreaPadding;
140+
const auto& contentInsets = state.contentInsetAdjustment;
134141
auto swap = state.swapLeftAndRightInRTL;
135-
if (insets.top != 0 || insets.bottom != 0) {
142+
if (insets.top != 0 || insets.bottom != 0 || contentInsets.top != 0 ||
143+
contentInsets.bottom != 0) {
136144
auto top = padded(
137-
resolvedPadding(style, Edge::Top, layoutDirection, swap), insets.top);
145+
resolvedPadding(style, Edge::Top, layoutDirection, swap),
146+
insets.top,
147+
contentInsets.top);
138148
auto bottom = padded(
139149
resolvedPadding(style, Edge::Bottom, layoutDirection, swap),
140-
insets.bottom);
150+
insets.bottom,
151+
contentInsets.bottom);
141152
style.setPadding(Edge::Top, top);
142153
style.setPadding(Edge::Bottom, bottom);
143154
}
144-
if (insets.left != 0 || insets.right != 0) {
155+
if (insets.left != 0 || insets.right != 0 || contentInsets.left != 0 ||
156+
contentInsets.right != 0) {
145157
auto left = padded(
146-
resolvedPadding(style, Edge::Left, layoutDirection, swap), insets.left);
158+
resolvedPadding(style, Edge::Left, layoutDirection, swap),
159+
insets.left,
160+
contentInsets.left);
147161
auto right = padded(
148162
resolvedPadding(style, Edge::Right, layoutDirection, swap),
149-
insets.right);
163+
insets.right,
164+
contentInsets.right);
150165
// The logical edges take precedence over every other padding edge along
151166
// the row. The physical edges are cleared, since at layout they are copied
152167
// over the logical ones when left and right swap in RTL.

‎packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewShadowNode.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ extern const char ScrollContentViewComponentName[];
2121

2222
/*
2323
* `ShadowNode` for the <ScrollContentView> component, the content container of
24-
* a <ScrollView>. It pads its content by the safe area in its state, on top of
25-
* the padding its style sets.
24+
* a <ScrollView>. It lays its content out clear of the safe area in its state
25+
* by at least the padding its style sets: across the scrollable axis as
26+
* padding, and along it as what the padding exceeds UIKit's content inset by.
2627
*/
2728
class ScrollContentViewShadowNode final : public ConcreteViewShadowNode<
2829
ScrollContentViewComponentName,
@@ -48,10 +49,11 @@ class ScrollContentViewShadowNode final : public ConcreteViewShadowNode<
4849
void applySafeAreaPadding();
4950

5051
/*
51-
* Pads `style` by `state.safeAreaPadding`, on top of the padding `style`
52-
* resolves to on each edge, following the precedence Yoga gives the padding
53-
* edges for `layoutDirection`. An edge with a percentage padding keeps it
54-
* and gets no inset.
52+
* Raises the padding `style` resolves to on each edge to at least
53+
* `state.safeAreaPadding`, less `state.contentInsetAdjustment` where UIKit
54+
* already applies the safe area as a content inset, following the precedence
55+
* Yoga gives the padding edges for `layoutDirection`. An edge with a
56+
* percentage padding keeps it and gets no inset.
5557
*/
5658
static void padForSafeArea(yoga::Style &style, const ScrollContentViewState &state, LayoutDirection layoutDirection);
5759

‎packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollContentViewState.h‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ namespace facebook::react {
2525
class ScrollContentViewState final {
2626
public:
2727
/*
28-
* The safe area the content is laid out clear of, as padding on top of the
29-
* padding the style sets. The enclosing scroll view sets the pair of edges
30-
* across its scrollable axis.
28+
* The safe area the content is laid out clear of, as a floor on the padding
29+
* the style sets. The enclosing scroll view sets the pair of edges across
30+
* its scrollable axis.
3131
*/
3232
EdgeInsets safeAreaPadding{};
3333

34+
/*
35+
* The content inset UIKit adds for the safe area along the scrollable axis,
36+
* which the padding the style sets on those edges counts toward. The
37+
* enclosing scroll view sets the pair of edges along its scrollable axis.
38+
*/
39+
EdgeInsets contentInsetAdjustment{};
40+
3441
/*
3542
* Whether left and right swap in RTL, which decides the padding edges the
3643
* safe area adds to.
@@ -48,6 +55,11 @@ class ScrollContentViewState final {
4855
(Float)data["top"].getDouble(),
4956
(Float)data["right"].getDouble(),
5057
(Float)data["bottom"].getDouble()}),
58+
contentInsetAdjustment(
59+
{(Float)data["contentInsetLeft"].getDouble(),
60+
(Float)data["contentInsetTop"].getDouble(),
61+
(Float)data["contentInsetRight"].getDouble(),
62+
(Float)data["contentInsetBottom"].getDouble()}),
5163
swapLeftAndRightInRTL(data["swapLeftAndRightInRTL"].getBool())
5264
{
5365
}
@@ -56,6 +68,8 @@ class ScrollContentViewState final {
5668
{
5769
return folly::dynamic::object("left", safeAreaPadding.left)("top", safeAreaPadding.top)(
5870
"right", safeAreaPadding.right)("bottom", safeAreaPadding.bottom)(
71+
"contentInsetLeft", contentInsetAdjustment.left)("contentInsetTop", contentInsetAdjustment.top)(
72+
"contentInsetRight", contentInsetAdjustment.right)("contentInsetBottom", contentInsetAdjustment.bottom)(
5973
"swapLeftAndRightInRTL", swapLeftAndRightInRTL);
6074
}
6175
#endif

0 commit comments

Comments
 (0)