From 1d1cbc93c23d59930a296a71a7bbf35d931f8701 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Sat, 24 Oct 2020 11:59:25 -0700 Subject: [PATCH] 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 --- .../ScrollView/RCTScrollViewComponentView.mm | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index 848e26d2678..d8bdd35b03f 100644 --- a/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -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