2323#import " RCTCustomPullToRefreshViewProtocol.h"
2424#import " RCTEnhancedScrollView.h"
2525#import " RCTFabricComponentsPlugins.h"
26+ #import " RCTScrollContentViewComponentView.h"
2627#import " RCTVirtualViewContainerState.h"
2728
2829using namespace facebook ::react;
@@ -449,9 +450,10 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
449450 } else if (contentInsetAdjustmentBehavior == ContentInsetAdjustmentBehavior::Always) {
450451 scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
451452 }
453+ ((RCTEnhancedScrollView *)_scrollView).padsContentForSafeArea =
454+ contentInsetAdjustmentBehavior != ContentInsetAdjustmentBehavior::Never;
452455 _shouldUpdateContentInsetAdjustmentBehavior = NO ;
453456 }
454- scrollView.padsContentForSafeArea = newScrollViewProps.padsContentForSafeArea ;
455457
456458 MAP_SCROLL_VIEW_PROP (disableIntervalMomentum);
457459 MAP_SCROLL_VIEW_PROP (snapToInterval);
@@ -464,6 +466,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
464466
465467 // Must run after `super updateProps:` so it is based on the latest calculated view.
466468 ((RCTEnhancedScrollView *)_scrollView).inverted = [self isInverted ];
469+ [self _updateContentViewSafeAreaPadding ];
467470}
468471
469472- (void )updateState : (const State::Shared &)state oldState : (const State::Shared &)oldState
@@ -563,6 +566,7 @@ - (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childCompone
563566 [_containerView insertSubview: childComponentView atIndex: index];
564567 if (![childComponentView conformsToProtocol: @protocol (RCTCustomPullToRefreshViewProtocol)]) {
565568 _contentView = childComponentView;
569+ [self _updateContentViewSafeAreaPadding ];
566570 }
567571}
568572
@@ -866,6 +870,46 @@ - (void)didMoveToWindow
866870 // The view is being removed, ensure that the scroll end event is dispatched
867871 [self _handleScrollEndIfNeeded ];
868872 }
873+ [self _updateContentViewSafeAreaPadding ];
874+ }
875+
876+ - (void )safeAreaInsetsDidChange
877+ {
878+ [super safeAreaInsetsDidChange ];
879+ [self _updateContentViewSafeAreaPadding ];
880+ }
881+
882+ - (void )layoutSubviews
883+ {
884+ [super layoutSubviews ];
885+ // Moving or resizing the view changes its insets without `safeAreaInsetsDidChange` firing; that only reports
886+ // window-level changes.
887+ [self _updateContentViewSafeAreaPadding ];
888+ }
889+
890+ /*
891+ * Hands the content container the safe area to pad its content by: the pair of insets across the scrollable axis,
892+ * unless `contentInsetAdjustmentBehavior` is `never`. Along the scrollable axis UIKit turns the safe area into content
893+ * insets instead, so content scrolls under the system UI.
894+ */
895+ - (void )_updateContentViewSafeAreaPadding
896+ {
897+ if (![_contentView isKindOfClass: [RCTScrollContentViewComponentView class ]]) {
898+ return ;
899+ }
900+ const auto &props = static_cast <const ScrollViewProps &>(*_props);
901+ UIEdgeInsets padding = UIEdgeInsetsZero;
902+ if (props.contentInsetAdjustmentBehavior != ContentInsetAdjustmentBehavior::Never) {
903+ UIEdgeInsets insets = self.safeAreaInsets ;
904+ if (props.horizontal ) {
905+ padding.top = insets.top ;
906+ padding.bottom = insets.bottom ;
907+ } else {
908+ padding.left = insets.left ;
909+ padding.right = insets.right ;
910+ }
911+ }
912+ ((RCTScrollContentViewComponentView *)_contentView).safeAreaPadding = padding;
869913}
870914
871915- (void )_handleScrollEndIfNeeded
0 commit comments