Skip to content

Commit

Permalink
Fixed ScrollView's .scrollToEnd to refrain from exceeding start bound…
Browse files Browse the repository at this point in the history
…ary.

Summary:
Thanks for submitting a pull request! Please provide enough information so that others can review your pull request:

> **Unless you are a React Native release maintainer and cherry-picking an *existing* commit into a current release, ensure your pull request is targeting the `master` React Native branch.**

Explain the **motivation** for making this change. What existing problem does the pull request solve?

The problem occurs when a ScrollView's content height is smaller than the ScrollView height.  If the method `scrollToEnd` is called on the ScrollView, it will pull the content down until the bottom of the content is aligned with the bottom of the Scrollview container.

This fix will ensure the proper functionality: That the furthest the ScrollView can scroll down is to where the top of the content container is at the origin (i.e., the ScrollView scroll number cannot be less than 0).

Prefer **small pull requests**. These are much easier to review and more likely to get merged. Make sure the PR does only one thing, otherwise please split it.

**Test plan (required)**

Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI.

Make sure tests pass on both Travis and Circle CI.

I tested on a scenario where the ScrollView is almost the full size of the screen, and the content of the ScrollView has a height of much less.  In this situation, the `scrollToEnd` method was executed and the content stayed in the same position.  This is the intended behavior.  If the content of the ScrollView is smaller than the height of the ScrollView, then the `scrollToEnd` method should not scroll anywhere.

**Code formatting**

Look around. Match the style of the rest of the codebase. See also the simple [style guide](https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#style-guide).

For more info, see the ["Pull Requests" section of our "Contributing" guidelines](https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#pull-requests).
Closes facebook#12889

Reviewed By: javache

Differential Revision: D5289894

Pulled By: sahrens

fbshipit-source-id: df2e779ee855c1dea85d33649d754371ad244bca
  • Loading branch information
Guardiannw authored and facebook-github-bot committed Jun 22, 2017
1 parent 4566f01 commit 5026040
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions React/Views/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ - (void)scrollToEnd:(BOOL)animated
BOOL isHorizontal = [self isHorizontal:_scrollView];
CGPoint offset;
if (isHorizontal) {
CGFloat offsetX = _scrollView.contentSize.width - _scrollView.bounds.size.width;
offset = CGPointMake(MAX(offsetX, 0), 0);
CGFloat offsetX = _scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right;
offset = CGPointMake(fmax(offsetX, 0), 0);
} else {
CGFloat offsetY = _scrollView.contentSize.height - _scrollView.bounds.size.height;
offset = CGPointMake(0, MAX(offsetY, 0));
CGFloat offsetY = _scrollView.contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom;
offset = CGPointMake(0, fmax(offsetY, 0));
}
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
// Ensure at least one scroll event will fire
Expand Down

0 comments on commit 5026040

Please sign in to comment.