diff --git a/React/Views/ScrollView/RCTScrollContentView.m b/React/Views/ScrollView/RCTScrollContentView.m index cf6a0b1f17953d..8006540a70f136 100644 --- a/React/Views/ScrollView/RCTScrollContentView.m +++ b/React/Views/ScrollView/RCTScrollContentView.m @@ -26,7 +26,7 @@ - (void)reactSetFrame:(CGRect)frame RCTAssert([scrollView isKindOfClass:[RCTScrollView class]], @"Unexpected view hierarchy of RCTScrollView component."); - [scrollView updateContentOffsetIfNeeded]; + [scrollView updateContentSizeIfNeeded]; } @end diff --git a/React/Views/ScrollView/RCTScrollView.h b/React/Views/ScrollView/RCTScrollView.h index 4a508ee1e9d8a2..765d3b3378a5a6 100644 --- a/React/Views/ScrollView/RCTScrollView.h +++ b/React/Views/ScrollView/RCTScrollView.h @@ -28,12 +28,6 @@ */ @property (nonatomic, readonly) UIView *contentView; -/** - * If the `contentSize` is not specified (or is specified as {0, 0}, then the - * `contentSize` will automatically be determined by the size of the subview. - */ -@property (nonatomic, assign) CGSize contentSize; - /** * The underlying scrollView (TODO: can we remove this?) */ @@ -68,7 +62,7 @@ @interface RCTScrollView (Internal) -- (void)updateContentOffsetIfNeeded; +- (void)updateContentSizeIfNeeded; @end diff --git a/React/Views/ScrollView/RCTScrollView.m b/React/Views/ScrollView/RCTScrollView.m index c52f449c730015..c9fe67bb9edbbe 100644 --- a/React/Views/ScrollView/RCTScrollView.m +++ b/React/Views/ScrollView/RCTScrollView.m @@ -301,7 +301,6 @@ - (instancetype)initWithEventDispatcher:(id)eventDis _automaticallyAdjustContentInsets = YES; _contentInset = UIEdgeInsetsZero; - _contentSize = CGSizeZero; _lastClippedToRect = CGRectNull; _scrollEventThrottle = 0.0; @@ -381,7 +380,7 @@ - (void)didUpdateReactSubviews - (void)didSetProps:(NSArray *)changedProps { if ([changedProps containsObject:@"contentSize"]) { - [self updateContentOffsetIfNeeded]; + [self updateContentSizeIfNeeded]; } } @@ -799,8 +798,6 @@ - (UIView *)viewForZoomingInScrollView:(__unused UIScrollView *)scrollView return _contentView; } -#pragma mark - Setters - - (CGSize)_calculateViewportSize { CGSize viewportSize = self.bounds.size; @@ -813,71 +810,16 @@ - (CGSize)_calculateViewportSize return viewportSize; } -- (CGPoint)calculateOffsetForContentSize:(CGSize)newContentSize -{ - CGPoint oldOffset = _scrollView.contentOffset; - CGPoint newOffset = oldOffset; - - CGSize oldContentSize = _scrollView.contentSize; - CGSize viewportSize = [self _calculateViewportSize]; - - BOOL fitsinViewportY = oldContentSize.height <= viewportSize.height && newContentSize.height <= viewportSize.height; - if (newContentSize.height < oldContentSize.height && !fitsinViewportY) { - CGFloat offsetHeight = oldOffset.y + viewportSize.height; - if (oldOffset.y < 0) { - // overscrolled on top, leave offset alone - } else if (offsetHeight > oldContentSize.height) { - // overscrolled on the bottom, preserve overscroll amount - newOffset.y = MAX(0, oldOffset.y - (oldContentSize.height - newContentSize.height)); - } else if (offsetHeight > newContentSize.height) { - // offset falls outside of bounds, scroll back to end of list - newOffset.y = MAX(0, newContentSize.height - viewportSize.height); - } - } - - BOOL fitsinViewportX = oldContentSize.width <= viewportSize.width && newContentSize.width <= viewportSize.width; - if (newContentSize.width < oldContentSize.width && !fitsinViewportX) { - CGFloat offsetHeight = oldOffset.x + viewportSize.width; - if (oldOffset.x < 0) { - // overscrolled at the beginning, leave offset alone - } else if (offsetHeight > oldContentSize.width && newContentSize.width > viewportSize.width) { - // overscrolled at the end, preserve overscroll amount as much as possible - newOffset.x = MAX(0, oldOffset.x - (oldContentSize.width - newContentSize.width)); - } else if (offsetHeight > newContentSize.width) { - // offset falls outside of bounds, scroll back to end - newOffset.x = MAX(0, newContentSize.width - viewportSize.width); - } - } - - // all other cases, offset doesn't change - return newOffset; -} - -/** - * Once you set the `contentSize`, to a nonzero value, it is assumed to be - * managed by you, and we'll never automatically compute the size for you, - * unless you manually reset it back to {0, 0} - */ - (CGSize)contentSize { - if (!CGSizeEqualToSize(_contentSize, CGSizeZero)) { - return _contentSize; - } - return _contentView.frame.size; } -- (void)updateContentOffsetIfNeeded +- (void)updateContentSizeIfNeeded { CGSize contentSize = self.contentSize; if (!CGSizeEqualToSize(_scrollView.contentSize, contentSize)) { - // When contentSize is set manually, ScrollView internals will reset - // contentOffset to {0, 0}. Since we potentially set contentSize whenever - // anything in the ScrollView updates, we workaround this issue by manually - // adjusting contentOffset whenever this happens - CGPoint newOffset = [self calculateOffsetForContentSize:contentSize]; _scrollView.contentSize = contentSize; - _scrollView.contentOffset = newOffset; } }