react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[docs] ScrollView height clarification

Open bistacos opened this issue 11 years ago • 65 comments

Currently, the only way I can implement a working ScrollView is if it's nested within a view of bounded height or {flex: 1}. The docs make it sound as if only the height of the ScrollView itself needs to be set in order to function.

I could be implementing it wrong but rendering a plain ScrollView with a set height that's not nested produces a ScrollView that bounces but does not scroll.

bistacos avatar Oct 14 '15 20:10 bistacos

I had the same issue as @mariotacke response above.

Do you understand why we would have to wrap this in a View? Works for me, but I still don't understand why setting the contentContainerStyle or style to flex: 0 wouldn't work. I guess because there is another container that is automatically set to 1 internally?

tyricec avatar Aug 23 '17 00:08 tyricec

I am on RN 0.47 and none of the options you guys provided worked for me. I tried setting static height on the ScrollView itself, around, within and still nothing. It bounces to the top.

wmonecke avatar Aug 25 '17 18:08 wmonecke

RN 0.48.2: When trying to create a horizontal scrollview that takes the minimal vertical space possible, the solution was to set flexGrow:0 on the ScrollView. Setting flex:0 on it did not have any effect for reasons unknown.

mtkopone avatar Sep 14 '17 13:09 mtkopone

rn 0.45.1:It seems that only the first Scrollview will appear this phenomenon, and adding an extra empty Scrollview can solve the problem。

dushaobindoudou avatar Sep 22 '17 09:09 dushaobindoudou

@dushaobindoudou trick helped me solve the issue

cchitsiang avatar Sep 23 '17 06:09 cchitsiang

"react-native": "^0.48.4",

flex:1 working but not scrolling, flexGrow:1 working and scrolling, flexShrink:1 working but not scrolling, flexBasis:1 not working

i think it should be like this.

<ScrollView contentContainerStyle={{flexGrow:1}}>
     <View style={{flex:1}}><View>
</ScrollView>

this is working in my project.

metinsut avatar Oct 06 '17 09:10 metinsut

Mine works with exactly as shown by @mtnst. Here is my scrolling view component:

import React from 'react'
import { StyleSheet, ScrollView } from 'react-native'
import * as Animatable from 'react-native-animatable'

const E1ScrollView = ({ children, animation, bottomBorder, style }) => {
    const { container, E1bottomBorder } = styles

    return (
        <ScrollView
            contentContainerStyle={{ flexGrow: 1 }}
            scrollEnabled>

            <Animatable.View
                style={[container, (bottomBorder) ? E1bottomBorder : null, style]}
                animation={animation}
                iterationCount={1}>

                {children}
            </Animatable.View>
        </ScrollView>
    )
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F0F0F0',
        flexDirection: 'column'
    },
    E1bottomBorder: {
        borderBottomWidth: 5,
        borderColor: '#DD0426',
    }
})

export { E1ScrollView }

If you would like to sample it, simply import that "common" component in to whatever screen you plan to use it and do this:

import { E1ScrollView } from '../common'
// ...
// Notice how you can overwrite styles by adding style={{ backgroundColor: 'red' }} to <E1ScrollView />
return (
     <E1ScrollView animation="fadeIn" bottomBorder>
        <View style={{ flex: 0 }}><Text>test</Text></View>
        <View style={{ flex: 0, flexDirection: 'column' }}>
            <Text>test</Text>
            <Text>test</Text>
            <Text>test</Text>
        </View>
        <View style={{ flex: 1 }} />
        <View style={{ flex: 0 }}><Text>test</Text></View>
    </E1ScrollView>
)

The part I would like to make sure you're aware of is you can create <CardSection /> View elements that have either flex: 0 or flex: 1 style and you will get effortless stacking. Then, you just need to work with margins and padding.

The <View style={{ flex: 1 }} /> element as I demonstrated above is a critical design element to be aware of, in my opinion. I found it somewhere along my journeys and it makes filling areas pretty effortless.

If your screen receives props that add DOM elements, your view will respond in an expected manner.

amackintosh avatar Oct 21 '17 22:10 amackintosh

@mariotacke you could just apply flexGrow: 0 to your ScrollView

logovaser avatar Dec 05 '17 17:12 logovaser

None of these tricks worked for me. I ended up just adding the style paddingBottom: 600 to the ScrollView to force it to fill the screen.

teckel12 avatar Dec 07 '17 16:12 teckel12

@teckel12 my responce was for the opposite problem. In the current version of react native ScrollView has flexGrow: 1 as default and fills all space (for some reason, View hasn't that). In your case you should only ensure that all your ScrollView`s containers have flex: 1. I could help you more if you provide your layout example.

logovaser avatar Dec 07 '17 16:12 logovaser

If I set the style of ScrollView to flex: 1 it doesn't render anything at all. It's like it's one pixel high.

teckel12 avatar Dec 07 '17 17:12 teckel12

It seems that the trick is to making EVERY parent View to have a style of flex: 1, not just the first parent View. When doing that, ScrollView doesn't need the flex: 1 either.

teckel12 avatar Dec 07 '17 17:12 teckel12

@teckel12 sure. As I said.

logovaser avatar Dec 07 '17 17:12 logovaser

This mix work for me: 1 View Global with "flex: 1", 1 View for ScrollView with "flex: 1", 1 ScrollView with "contentContainerStyle={{flexGrow:1}"

render() {
    return (
      // Global View
      <View style={{flex: 1}}>
        {/* View for ScrollView */}
        <View style={{flex: 1}}>
          <ScrollView contentContainerStyle={{flexGrow:1}}>
            <View style={{flex: 1, alignItems: 'center' }}>
              {/* More things */}
            </View>
          </ScrollView>
        </View>
        {/* Another Component */}
      </View>
    );
  }

zicrox avatar Dec 11 '17 14:12 zicrox

@zicrox I also needed to use <ScrollView contentContainerStyle={{ flexGrow: 1 }}> along with all parent containers using style={{flex: 1}}. I needed to use React Developer Tools to diagnose and get this resolved.

teckel12 avatar Dec 11 '17 15:12 teckel12

@teckel12 think it is run well with all parent containers using style={{flex: 1}} as long asScrollView is inside of a parent with<View style={{flex: 1}}> 🤔

zicrox avatar Dec 11 '17 19:12 zicrox

In version 0.50.3, I experience a problem with ScrollView where it doesn't let me scroll to the last few items. Like I have 45 records on the list and on reaching 41 I can see the list bouncing but not showing the rest. When it bounces I can see the rest of the records rendered. Tried using ScrollView, ListView, and FlatList none of them worked.

venk-aw avatar Dec 17 '17 23:12 venk-aw

@venk-aw

I also have the same problem but only if I set height in child components using percentage value.

nikolal avatar Dec 18 '17 00:12 nikolal

@nikolal can confirm, also other dynamic height attributes such as minHeight. Trying to find out what causes this, looks like a precision error.

ptmt avatar Dec 22 '17 13:12 ptmt

@ptmt

Yes, I created separate issue for this:

https://github.com/facebook/react-native/issues/17257

nikolal avatar Dec 22 '17 13:12 nikolal