From 2ea4bcd0010c590755aebd0043dc58bb4458e340 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 20 Feb 2019 20:28:08 -0800 Subject: [PATCH] Fixed ScrollView adjust inset behavior (#23555) Summary: Fixes #23500 , if user set prop `contentInsetAdjustmentBehavior="automatic"` of ScrollView, it not works when initial on the screen. We fixes it by filter valid offset, if offset is valid, just return. [iOS] [Fixed] - Fixed ScrollView adjust inset behavior Pull Request resolved: https://github.com/facebook/react-native/pull/23555 Differential Revision: D14161593 Pulled By: cpojer fbshipit-source-id: 01434e55106ffde7f8e39f66dd5b0f02df9b38b1 --- React/Views/ScrollView/RCTScrollView.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/React/Views/ScrollView/RCTScrollView.m b/React/Views/ScrollView/RCTScrollView.m index d143c73deb24f5..f64e2f669976f7 100644 --- a/React/Views/ScrollView/RCTScrollView.m +++ b/React/Views/ScrollView/RCTScrollView.m @@ -323,11 +323,22 @@ - (void)setFrame:(CGRect)frame if (CGSizeEqualToSize(contentSize, CGSizeZero)) { self.contentOffset = originalOffset; } else { - // Make sure offset don't exceed bounds. This could happen on screen rotation. + if (@available(iOS 11.0, *)) { + if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, self.adjustedContentInset)) { + contentInset = self.adjustedContentInset; + } + } CGSize boundsSize = self.bounds.size; + CGFloat xMaxOffset = contentSize.width - boundsSize.width + contentInset.right; + CGFloat yMaxOffset = contentSize.height - boundsSize.height + contentInset.bottom; + // Make sure offset doesn't exceed bounds. This can happen on screen rotation. + if ((originalOffset.x >= -contentInset.left) && (originalOffset.x <= xMaxOffset) && + (originalOffset.y >= -contentInset.top) && (originalOffset.y <= yMaxOffset)) { + return; + } self.contentOffset = CGPointMake( - MAX(-contentInset.left, MIN(contentSize.width - boundsSize.width + contentInset.right, originalOffset.x)), - MAX(-contentInset.top, MIN(contentSize.height - boundsSize.height + contentInset.bottom, originalOffset.y))); + MAX(-contentInset.left, MIN(xMaxOffset, originalOffset.x)), + MAX(-contentInset.top, MIN(yMaxOffset, originalOffset.y))); } }