From 9c1c5a7455d90ec837a9a6141c096de70b798e43 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 20 Feb 2019 21:28:11 -0800 Subject: [PATCH] Fix scrollview over bounds of content size (#23427) Summary: Fix scrollview `offset` out of content size in iOS, Android uses `scrollTo` and `smoothScrollTo` which not have this issue. Fixes like #13594 #22768 #19970 . [iOS] [Fixed] - Fixed scrollView offset out of content size. Pull Request resolved: https://github.com/facebook/react-native/pull/23427 Differential Revision: D14162663 Pulled By: cpojer fbshipit-source-id: a95371c8d703b6d5f604af0072f86c01c2018f4a --- React/Views/ScrollView/RCTScrollView.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/React/Views/ScrollView/RCTScrollView.m b/React/Views/ScrollView/RCTScrollView.m index f64e2f669976f7..2ed87866bd888e 100644 --- a/React/Views/ScrollView/RCTScrollView.m +++ b/React/Views/ScrollView/RCTScrollView.m @@ -604,8 +604,19 @@ - (void)scrollToOffset:(CGPoint)offset - (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated { if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) { + CGRect maxRect = CGRectMake(fmin(-_scrollView.contentInset.left, 0), + fmin(-_scrollView.contentInset.top, 0), + fmax(_scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right + fmax(_scrollView.contentInset.left, 0), 0.01), + fmax(_scrollView.contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom + fmax(_scrollView.contentInset.top, 0), 0.01)); // Make width and height greater than 0 // Ensure at least one scroll event will fire _allowNextScrollNoMatterWhat = YES; + if (!CGRectContainsPoint(maxRect, offset)) { + CGFloat x = fmax(offset.x, CGRectGetMinX(maxRect)); + x = fmin(x, CGRectGetMaxX(maxRect)); + CGFloat y = fmax(offset.y, CGRectGetMinY(maxRect)); + y = fmin(y, CGRectGetMaxY(maxRect)); + offset = CGPointMake(x, y); + } [_scrollView setContentOffset:offset animated:animated]; } }