Prevent ScrollTo view command from scrolling outside of content view

Summary:
Changelog: [internal]

ScrollTo command needs to clamp the value in order to not scroll out of content view of scroll view. Paper does the same thing.

Reviewed By: JoshuaGross

Differential Revision: D24526992

fbshipit-source-id: 47c48ecb01f4ae40791306016dd7847079263128
This commit is contained in:
Samuel Susla
2020-10-24 12:02:07 -07:00
committed by Facebook GitHub Bot
parent 46eb3ec474
commit 1d1cbc93c2
@@ -445,7 +445,28 @@ static void RCTSendPaperScrollEvent_DEPRECATED(UIScrollView *scrollView, NSInteg
- (void)scrollTo:(double)x y:(double)y animated:(BOOL)animated
{
[_scrollView setContentOffset:CGPointMake(x, y) animated:animated];
CGPoint offset = CGPointMake(x, y);
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
if (!CGRectContainsPoint(maxRect, offset)) {
CGFloat localX = fmax(offset.x, CGRectGetMinX(maxRect));
localX = fmin(localX, CGRectGetMaxX(maxRect));
CGFloat localY = fmax(offset.y, CGRectGetMinY(maxRect));
localY = fmin(localY, CGRectGetMaxY(maxRect));
offset = CGPointMake(localX, localY);
}
[_scrollView setContentOffset:offset animated:animated];
}
}
- (void)scrollToEnd:(BOOL)animated