Skip to content

Commit f6516d2

Browse files
zhongwuzwgrabbou
authored andcommitted
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: #23427 Differential Revision: D14162663 Pulled By: cpojer fbshipit-source-id: a95371c8d703b6d5f604af0072f86c01c2018f4a
1 parent 699fad7 commit f6516d2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

React/Views/ScrollView/RCTScrollView.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,19 @@ - (void)scrollToOffset:(CGPoint)offset
588588
- (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
589589
{
590590
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
591+
CGRect maxRect = CGRectMake(fmin(-_scrollView.contentInset.left, 0),
592+
fmin(-_scrollView.contentInset.top, 0),
593+
fmax(_scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right + fmax(_scrollView.contentInset.left, 0), 0.01),
594+
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
591595
// Ensure at least one scroll event will fire
592596
_allowNextScrollNoMatterWhat = YES;
597+
if (!CGRectContainsPoint(maxRect, offset)) {
598+
CGFloat x = fmax(offset.x, CGRectGetMinX(maxRect));
599+
x = fmin(x, CGRectGetMaxX(maxRect));
600+
CGFloat y = fmax(offset.y, CGRectGetMinY(maxRect));
601+
y = fmin(y, CGRectGetMaxY(maxRect));
602+
offset = CGPointMake(x, y);
603+
}
593604
[_scrollView setContentOffset:offset animated:animated];
594605
}
595606
}

0 commit comments

Comments
 (0)