Skip to content

Commit

Permalink
Fix content offset validation
Browse files Browse the repository at this point in the history
Summary:
Content offset was broken because on initial render contentSize is {0,0} so any positive offset is lost. Also inset top/bottom and left/right were inversed �, this led to bad initial scrolling offset when using contentInset. This fixes it by making sure contentSize is actually measured (not {0,0}. I guess it's possible that the content is ACTUALLY {0,0} but in that case I don't think it really matters).

**Test plan**
Tested that a scrollview has proper scroll position when specifying contentOffset. Also tested that it works well with contentInset.
```js
<ScrollView contentOffset={{y: 100}}>
  <View style={{height: 1000}} />
</ScrollView>
```
Closes #15670

Differential Revision: D5771221

Pulled By: shergin

fbshipit-source-id: 455ed8fd5a4ad1ec61780b573d1a8ef1d77dd124
  • Loading branch information
janicduplessis authored and facebook-github-bot committed Sep 5, 2017
1 parent ed31f7a commit 64be883
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions React/Views/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,17 @@ - (void)setFrame:(CGRect)frame

UIEdgeInsets contentInset = self.contentInset;
CGSize contentSize = self.contentSize;

CGSize boundsSize = self.bounds.size;

self.contentOffset = CGPointMake(
MAX(-contentInset.top, MIN(contentSize.width - boundsSize.width + contentInset.bottom, originalOffset.x)),
MAX(-contentInset.left, MIN(contentSize.height - boundsSize.height + contentInset.right, originalOffset.y)));

// If contentSize has not been measured yet we can't check bounds.
if (CGSizeEqualToSize(contentSize, CGSizeZero)) {
self.contentOffset = originalOffset;
} else {
// Make sure offset don't exceed bounds. This could happen on screen rotation.
CGSize boundsSize = self.bounds.size;
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)));
}
}

#if !TARGET_OS_TV
Expand Down

0 comments on commit 64be883

Please sign in to comment.