Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contentInsetAdjustmentBehavior set to automatic on ScrollView in the new architecture #34217

Closed
wants to merge 9 commits into from
Prev Previous commit
Next Next commit
Changed frame to 0 when recycled for large titles
Previous solution of recreating scroll view each time didn't work for the large-title-bug.  Go to large title, scroll to collapse, go back then go to large title and it would be collapsed - it would scroll slightly so could see the first line of content but it would be collapsed.
Stumbled across changing frame from 0 then back to old value and it fixes all the inset behavior auto bugs. Guess it resets everything - without resetting the frame it remembered some settings and made it go through a completely different path when remounting from when recycling
  • Loading branch information
grahammendick committed Jul 18, 2022
commit f28c6f6e46059c0faeb94d2765cc870a486d7b07
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ @implementation RCTScrollViewComponentView {
// This helps to only update state from `scrollViewDidScroll` in case
// some other part of the system scrolls scroll view.
BOOL _isUserTriggeredScrolling;
BOOL _createScrollView;
BOOL _shouldUpdateContentSize;
BOOL _shouldUpdateContentInsetAdjustmentBehavior;

CGPoint _contentOffsetWhenClipped;
CGRect _oldFrame;
}

+ (RCTScrollViewComponentView *_Nullable)findScrollViewComponentViewForView:(UIView *)view
Expand All @@ -116,21 +116,14 @@ - (instancetype)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const ScrollViewProps>();
_props = defaultProps;
_createScrollView = YES;
}

return self;
}
_oldFrame = frame;

- (void)ensureScrollView
{
if (_createScrollView) {
[_scrollView removeFromSuperview];
_scrollView = [[RCTEnhancedScrollView alloc] initWithFrame:self.bounds];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.delaysContentTouches = NO;
((RCTEnhancedScrollView *)_scrollView).overridingDelegate = self;
_isUserTriggeredScrolling = NO;
_shouldUpdateContentInsetAdjustmentBehavior = YES;
[self addSubview:_scrollView];

_containerView = [[UIView alloc] initWithFrame:CGRectZero];
Expand All @@ -139,8 +132,16 @@ - (void)ensureScrollView
[self.scrollViewDelegateSplitter addDelegate:self];

_scrollEventThrottle = INFINITY;
_createScrollView = NO;
}

return self;
}

- (void)dealloc
{
// Removing all delegates from the splitter nils the actual delegate which prevents a crash on UIScrollView
// deallocation.
[self.scrollViewDelegateSplitter removeAllDelegates];
}

- (RCTGenericDelegateSplitter<id<UIScrollViewDelegate>> *)scrollViewDelegateSplitter
Expand Down Expand Up @@ -179,7 +180,10 @@ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
[self ensureScrollView];
if (!CGRectEqualToRect(_oldFrame, CGRectNull)) {
[self setFrame:_oldFrame];
_oldFrame = CGRectNull;
}
const auto &oldScrollViewProps = *std::static_pointer_cast<const ScrollViewProps>(_props);
const auto &newScrollViewProps = *std::static_pointer_cast<const ScrollViewProps>(props);

Expand Down Expand Up @@ -270,7 +274,8 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
}
}

if (oldScrollViewProps.contentInsetAdjustmentBehavior != newScrollViewProps.contentInsetAdjustmentBehavior) {
if ((oldScrollViewProps.contentInsetAdjustmentBehavior != newScrollViewProps.contentInsetAdjustmentBehavior) ||
_shouldUpdateContentInsetAdjustmentBehavior) {
auto const contentInsetAdjustmentBehavior = newScrollViewProps.contentInsetAdjustmentBehavior;
if (contentInsetAdjustmentBehavior == ContentInsetAdjustmentBehavior::Never) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
Expand All @@ -281,6 +286,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
} else if (contentInsetAdjustmentBehavior == ContentInsetAdjustmentBehavior::Always) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
}
_shouldUpdateContentInsetAdjustmentBehavior = NO;
}

MAP_SCROLL_VIEW_PROP(disableIntervalMomentum);
Expand All @@ -306,13 +312,12 @@ - (void)updateState:(State::Shared const &)state oldState:(State::Shared const &

CGSize contentSize = RCTCGSizeFromSize(data.getContentSize());

if (!_shouldUpdateContentSize && CGSizeEqualToSize(_contentSize, contentSize)) {
if (CGSizeEqualToSize(_contentSize, contentSize)) {
return;
}

_contentSize = contentSize;
_containerView.frame = CGRect{RCTCGPointFromPoint(data.contentBoundingRect.origin), contentSize};
_shouldUpdateContentSize = NO;

[self _preserveContentOffsetIfNeededWithBlock:^{
self->_scrollView.contentSize = contentSize;
Expand All @@ -337,7 +342,6 @@ - (void)_preserveContentOffsetIfNeededWithBlock:(void (^)())block

- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
[self ensureScrollView];
[_containerView insertSubview:childComponentView atIndex:index];
}

Expand Down Expand Up @@ -394,13 +398,17 @@ - (void)_updateStateWithContentOffset

- (void)prepareForRecycle
{
const auto defaultProps = std::make_shared<const ScrollViewProps>();
_props = defaultProps;
const auto &props = *std::static_pointer_cast<const ScrollViewProps>(_props);
_scrollView.contentOffset = RCTCGPointFromPoint(props.contentOffset);
// We set the default behavior to "never" so that iOS
// doesn't do weird things to UIScrollView insets automatically
// and keeps it as an opt-in behavior.
_scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
_shouldUpdateContentInsetAdjustmentBehavior = YES;
_state.reset();
_isUserTriggeredScrolling = NO;
[self.scrollViewDelegateSplitter removeAllDelegates];
_createScrollView = YES;
_shouldUpdateContentSize = YES;
_oldFrame = self.frame;
[self setFrame:CGRectZero];
[super prepareForRecycle];
}

Expand Down