React-native: How can i scrollTo Bottom

Created on 18 Apr 2015  路  18Comments  路  Source: facebook/react-native

ScrollView how can i scroll to the bottom

Locked

Most helpful comment

Try this:

 <ScrollView ref="scrollView"
             onContentSizeChange={(width,height) => this.refs.scrollView.scrollTo({y:height})}>
 </ScrollView>

All 18 comments

Compute the y offset from the content size and viewport size, and call scrollTo.

but i can i get viewport size and content size ?

Look in the source code to learn how the UIManager.measureX methods are used. You can measure the scroll view and its content view. If you are using a ListView you will need to write your own ListView that measures the height of every single row to compute the content size.

Where do you call the scrollTo method?

@bdeezz, something like:

render() {
  return <ScrollView ref="scrollView" />;
}

onSomeEvent() {
  this.refs.scrollView.scrollTo(0); // will scroll to the top at y-position 0
}

I came up with this solution.

there is a component for listView to scroll to top, hope it is uesful to someone: scrolltotop

One way is to perform a transform on the ScrollView and the inner elements.
This allows you to to use the scrollTo(0) function

<View ref="container"
  style={{transform: [{rotate: '180deg'}] }} >
>
<ScrollView/>
</View>

Try this out: https://github.com/fritx/react-native-auto-scroll
Works fine with both iOS and Android (with adjustResize config'd).

// android/app/src/main/AndroidManifest.xml
       <activity
          android:name=".MainActivity"
          android:label="@string/app_name"
 +        android:windowSoftInputMode="adjustResize"
          android:configChanges="keyboard|keyboardHidden|orientation|screenSize">

Try this:

 <ScrollView ref="scrollView"
             onContentSizeChange={(width,height) => this.refs.scrollView.scrollTo({y:height})}>
 </ScrollView>

@VladPolynko this seems to work fine for me on Android, but on iOS it scrolls below the ScrollView content (as if there was no lower boundary for the ScrollView) (RN 0.38.0)

Same issue than @septemptus

Same issue as @septemptus
ScrollView in iOS has no lower boundary and gives a bigger height.
My shortcut is to give a manual offset for iOS-only.

scrolltoend method still scrolls past the end of the ScrollView on IOS.

  • ReactNative v0.47.2

onSomeEvent = () => {
this.scrollView.scrollTo({ x: 0});
}

This worksssss

Was this page helpful?
0 / 5 - 0 ratings