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

Experiencing the same thing. This sure seems like a bug to me, but if it's not the docs should definitely reflect that you need to nest it in another View and can't set height on the ScrollView directly.

Check this out to see it happen: https://rnplay.org/apps/pTCbwA

After you see the full screen ScrollView, just wrap the ScrollView in an unstyled view to see it display at the correct height.

marcshilling avatar Oct 22 '15 17:10 marcshilling

wasted too much time trying to figure it out :/ :+1: needs fix or doc update

farzd avatar Jan 13 '16 20:01 farzd

Neither my profile component or the dashboard component is scrolling. Even when i cut and paste the code from Branch 13 in this egghead.io tutorial Repo i still cant get scrolling. https://github.com/eggheadio/react-native-gh-notetaker/blob/13-finish-repositories-component/App/Components/Profile.js

This is the component code which works for rendering the data but does not scroll the data. Anything stand out here?

var React = require('react-native');
var Badge = require('./Badge');
var Separator = require('./Helpers/Separator');

var {
  Text,
  View,
  ListView,
  StyleSheet,
  ScrollView
} = React;

var styles = StyleSheet.create({
  container: {
    flex: 1
  },

  buttonText: {
    fontSize: 18,
    color: 'white',
    alignSelf: 'center'
  },
  rowContainer: {
    padding: 10
  },
  rowTitle: {
    color: '#48BBEC',
    fontSize: 16
  },
  rowContent: {
    fontSize: 19
  }
});

class Profile extends React.Component{
  getRowTitle(user, item){
    item = (item === 'public_repos') ? item.replace('_', ' ') : item;
    return item[0] ? item[0].toUpperCase() + item.slice(1) : item;
  }
  render(){
    var userInfo = this.props.userInfo;
    var topicArr = ['company', 'location', 'followers', 'following', 'email', 'bio', 'public_repos'];
    var list = topicArr.map((item, index) => {
      if(!userInfo[item]){
        return <View key={index}/>
      } else {
        return (
          <View key={index}>
            <View style={styles.rowContainer}>
              <Text style={styles.rowTitle}> {this.getRowTitle(userInfo, item)} </Text>
              <Text style={styles.rowContent}> {userInfo[item]} </Text>
            </View>
            <Separator />
          </View>
        )
      }
    });
    return (
      <ScrollView style={styles.container}>
        <Badge userInfo={this.props.userInfo}/>
        {list}
      </ScrollView>
    )
  }
};

Profile.propTypes = {
  userInfo: React.PropTypes.object.isRequired
}

module.exports = Profile;

https://jsfiddle.net/lokal/v7zdb16g/

Thank you.

idkjs avatar Jan 20 '16 04:01 idkjs

Exactely the same problem...

How to set the ScrollView to "take all the remaining space" instead of setting height: XX ?

Depending on the device which is being used, the remaining height varies a lot...

bebeun avatar Mar 03 '16 15:03 bebeun

How is this not trivial? I only get a working scrollView with fixed height so far (no specific scrollview wrapper needed), setting flex:1 on contentContainerStyle, style or wrapper style, setting it on any or all of them does not work for me. It just renders the entire view, overflowing the screen, no scroll.

edit: check all flex values on the page. In my case, there was another flex element above the scroll view that made things weird.

ThaJay avatar Jun 13 '16 10:06 ThaJay

I also have this issue...

nikolal avatar Jul 18 '16 10:07 nikolal

+1, btw, your views root container should contain flex: 1, to avoid the inner scrollview container to overflow off the screen

ospfranco avatar Jul 29 '16 12:07 ospfranco

Use contentContainerStyle={{flex:1}} on ScrollView tag, that should fix your problem.

AugustoL avatar Aug 24 '16 13:08 AugustoL

style={{ flex: 1}} isn't working for me. The content in our ScrollView is loaded in dynamically from an API call. The component that holds that dynamic content is resizing correctly, but it's as though the parent isn't taking that height change into account and I can't scroll to the bottom of our list. Do I need to do something with onLayout to notify that parent ScrollView of the height change?

gkrinc avatar Oct 04 '16 17:10 gkrinc

@gkrinc how are you setting the inner state of the scrollview once you receive your API call data? you can try triggering a re-render via this.setState({...}) or using this.forceUpdate().

ospfranco avatar Oct 12 '16 20:10 ospfranco

Thanks @ospfranco I got it working. I ended up with a static height on something that was causing the issue. Totally my fault :s

gkrinc avatar Oct 13 '16 15:10 gkrinc

I ended up solving this problem by wrapping the ScrollView in a View with {flex:1} and setting nothing about height in my ScrollView style nor contentContainerStyle. Takes the full height and goes deep down the screen.

But with this solution you cannot really rely on "flex" in direct children. If the screen isn't initially filled with content you'll have some unused spaces.

ancyrweb avatar Oct 15 '16 07:10 ancyrweb

I tried to set up a bounded height scrollview, but it seems like to automatically take up all remaining height of parent, the only way to do it for me is to use a wrapper view outside of scrollview

hitbear518 avatar Dec 20 '16 07:12 hitbear518

For me contentContainerStyle={{flex: 0}} did the trick.

alexindigo avatar Mar 07 '17 01:03 alexindigo

@alexindigo Makes sense because the size of the contentContainer should not flex, it should match the size of the contents.

DylanVann avatar Apr 16 '17 22:04 DylanVann

{flex: 1} works in 0.43. or {flexGrow: 1,flexShrink: 1, flexBasis: 1,}

njafei avatar May 02 '17 09:05 njafei

I also experience the same thing. Except that scrolling to bottom would not be actual bottom, I would have content cut off. Unless I wrap it with <View> and set flex:1. Setting flexGrow:1 on the view did not fix it for me. I would get this content cut off bug when adding a marginVertical to the ScrollView or FlastList too.

Noitidart avatar May 14 '17 17:05 Noitidart

In my case also the scrollview doesnot scrolls inside a view conmponent. Any solutions for it?

Chandhru08 avatar Jun 01 '17 18:06 Chandhru08

Just ran into this. I wanted a horizontal ScrollView to take as little height as possible. The ScrollView by itself displayed as if it had flex: 1 specified, which it didn't. Thanks to @bistacos, wrapping it in a View solves my problem.

mariotacke avatar Jun 21 '17 03:06 mariotacke

none of the solutions above worked for me, perhaps because we all tested these solutions on different versions of RN... I'm on RN47, and the only solution that worked for me, was manually measuring out the 3 different heights i needed, and then dynamically swapping between heights based on what content needed the extra height. So in the end it, this is what finally worked for me:

contentContainerStyle={{ height: userType === 'SHOPPER' ? 600 : userType === 'DIST' && !DistributorId ? 660 : DistributorId ? 860 : 700 }}

quadsurf avatar Aug 11 '17 16:08 quadsurf