37

inside my application index.io.js there is only 1 ScrollView that is wrapped inside a View under the render function. Sometimes I can scroll to the very bottom of the ScrollView and keep the screen there. However the ScrollView would bounce back to the top of the screen and fail to stay at the bottom sometimes.

Does anyone know what is happening? Thanks.

render() {
  return <View style={{flex: 1}}>
  <ScrollView>
    <Text>234</Text>
    <Text>234</Text>
    <Text>234</Text>
    <Text>234</Text>
    // .... repeat so many times ...
  </ScrollView>
  </View>
}

p.s: my RN is 0.28.0, iOS deployment target is 8.0

Shih-Min Lee
  • 9,350
  • 7
  • 37
  • 67

12 Answers12

56

For React native newbies like me:

lookout for making the error of setting {flex:1} on the scrollview's contentContainerStyle attribute, which is basically telling the scrollview explicitly not to scroll and expecting it to scroll, since this sets the content height to the same parent's frame which is the scrollview

TheFuquan
  • 1,737
  • 16
  • 18
  • 4
    for us, setting `contentContainerStyle={{ flex: 1}}` actually solved our issue! – Cole Jun 27 '17 at 20:59
  • 1
    In my case also the opposite, instead `contentContainerStyle={{ marginBottom: 5 }}` to the `ScrollView` having it wrapped into a `View` with `flex: 1`. – jesugmz Jan 03 '19 at 00:30
24

To answer the question exactly, the ScrollView should also have a style={{flex: 1}} prop. A ScrollView needs a set height to scroll.

However in my case, I had padding on my scroll view, and that must have thrown out some calculation deep in the system. I am using react native 0.55.

To resolve I needed to change:

<ScrollView style={{flex: 1, padding: 10}}>
   //Content goes here
</ScrollView>

to

<View style={{padding: 10, flex: 1}}>
   <ScrollView style={{flex: 1}}>
      //Content goes here
   </ScrollView>
</View>
Ryan Knell
  • 6,204
  • 2
  • 40
  • 32
24

contentContainerStyle={{ paddingBottom: 60 }}

so applying paddingBottom with a specific size should solve it for you

8

Adding an extra useless view on the bottom of scrollView :

<ScrollView>
//your contents here

  <View style={{height:100(or any suitable value)}} />
</ScrollView>

solved mine issue.

Redmen Ishab
  • 2,199
  • 18
  • 22
  • This was the only solution that worked for me as well. For some reason the ScrollView thought its total height was 100 pixels smaller than it actually was and in doing so, cut off the bottommost view and half of the one above it ¯\_(ツ)_/¯ – instanceof Sep 12 '19 at 17:30
5

Keep in mind that ScrollViews must have a bounded height in order to work, since they contain unbounded-height children into a bounded container (via a scroll interaction). In order to bound the height of a ScrollView, either set the height of the view directly (discouraged) or make sure all parent views have bounded height. Forgetting to transfer {flex: 1} down the view stack can lead to errors here, which the element inspector makes easy to debug.

https://facebook.github.io/react-native/docs/scrollview.html

I think you should assign a style to your scrollView with flex:1 property. And create a contentContainerStyle to have a better design.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Burak Karasoy
  • 1,682
  • 2
  • 21
  • 33
5

When you're using in react native, use flexGrow instead of flex. If you want a more in depth guide as of why, check out the article below:

https://medium.com/@peterpme/taming-react-natives-scrollview-with-flex-144e6ff76c08

Philip Aarseth
  • 301
  • 4
  • 16
4

Solution tested on react 0.57

IDK whether you fixed the issue but this solution worked for me

<ScrollView contentContainerStyle={{ padding: 10 }}>
    // User contents here
</ScrollView>

No need to flex anything. This piece of code will generate a view with gutter on all the 4 sides. And the cool thing is that the last element is not getting clipped.

Check the live demo here

Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
2

This happened to me when I used position: "absolute". My solution was to use the parent view {position: "relative"}. Solved my problem.

bobbydyr
  • 21
  • 1
  • 2
0
<ScrollView style={{flex: 1}}>
  <View style={styles.bottom}>
    {
       list.size > 0 && list.map((_, index) => {
        return (
          <View style={styles.listItem} key={index}>
            <Text style={styles.count}>{index + 1}</Text>
          </View>
        )
      })
    }
  </View>
</ScrollView>

use flex: 1

freedom
  • 110
  • 2
  • 11
0
<View style={{height:'100%',width:'100%',backgroundColor:'#fff'}}>
     <ScrollView>
           //// content here 
     </ScrollView>
</View>

This works for me

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

It can also happen if you accidentally apply paddings to regular ScrollView style prop instead of contentContainerStyle

Aliaksei
  • 1,094
  • 11
  • 20
0

Avoid applying UI styles (eg. padding), directly to Scrollview's style props, it distorts React Native's rendering of it. Instead, apply your styles to contentContainerStyle. This works 100% of the time.

DevErnie_
  • 1
  • 2