React native, children of ScrollView wont fill full height

FlexboxReact Native

Flexbox Problem Overview


So I have a horizontal scrollview at the top of the view. The ScrollView contains nodes that have a specified width. I then have a border on the bottom of the ScrollView, like you can see in this screen cap: http://i.imgur.com/pOV1JFP.png

As you can see the child nodes of the ScrollView at the top don't reach the border. However, if I change the ScrollView to a View with flexDirection: 'row', then the child nodes fill the height fine. I've tried changing a few properties on the scrollview, such as:

  • Setting padding:0 on contentContainerStyle
  • automaticallyAdjustContentInsets={false}
  • Changing the values of contentInsets directly

None of those seem to fix the issue.

The scrollview code & style:

var styles = StyleSheet.create({
  nav: {
    padding: 0,
    marginTop: 30,
    flex: 1,
    borderBottomWidth: 1,
    borderColor: '#000000'
  }
});

<ScrollView
  style={[{width: screen.width}, styles.nav]}
  horizontal={true}
  showsHorizontalScrollIndicator={true}
  automaticallyAdjustContentInsets={false}>
  {dayNavItems}
</ScrollView>

The child components (makes up dayNavItems)

const styles = StyleSheet.create({
  container: {
    paddingLeft: 15,
    paddingRight: 15,
    width: 50,
    justifyContent: 'center'
  },
  odd: {
    backgroundColor: '#ccc'
  },
  selected: {
    backgroundColor: '#39b54a'
  },
  text: {
    fontFamily: 'Optima'
  }
});

class DayNavItem extends React.Component {
  static propTypes = {
    day: React.PropTypes.object.isRequired,
    odd: React.PropTypes.bool,
    selectDay: React.PropTypes.func.isRequired,
    selected: React.PropTypes.bool
  };

  render() {
    const d = new Date(this.props.day.created_at);
    const viewStyles = [styles.container];
    if (this.props.selected) {
      viewStyles.push(styles.selected);
    } else if (this.props.odd) {
      viewStyles.push(styles.odd);
    }
    return (
      <TouchableOpacity style={viewStyles} onPress={() => this.props.selectDay(this.props.day.uuid)}>
        <View>
          <Text style={styles.text}>{getMonth(d)}</Text>
          <Text style={styles.text}>{d.getDate()}</Text>
        </View>
      </TouchableOpacity>
    );
  }
}

Flexbox Solutions


Solution 1 - Flexbox

Adding contentContainerStyle={{flexGrow: 1}} will do the trick.

<ScrollView contentContainerStyle={{flexGrow: 1}}>

</ScrollView>

Solution 2 - Flexbox

There is a property called contentContainerStyle for ScrollView. I just solved a similar issue where I set flex: 1 to the ScrollView's styles, ScrollView's contentContainerStyle, and the child View component.

Solution 3 - Flexbox

The other answers are correct, but just to provide a clarifying example:

<ScrollView contentContainerStyle={{ flex: 1 }}>
  {children}
</ScrollView>

Solution 4 - Flexbox

Setting flex: 1 for ScrollView's contentContainerStyle property did the trick for me.

Solution 5 - Flexbox

If ScrollView is wrapped inside of a SafeAreaView, put flex: 1 on SafeAreaView, this did the trick for me after hours of debugging...

Parent:

enter image description here

Child (styles ScrollView I implemented has nothing to do with the current issue you reported):

enter image description here

Solution 6 - Flexbox

I always end up creating this component in all my projects:

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

export function FullHeightScrollView(
  props: {
    children: React.ReactNode
  } & Omit<ScrollViewProps, 'contentContainerStyle'>
) {
  return (
    <ScrollView contentContainerStyle={styles.grow} {...props}>
      {props.children}
    </ScrollView>
  )
}

const styles = StyleSheet.create({
  grow: { flexGrow: 1 },
})

Use it in place of React Native's ScrollView.

Solution 7 - Flexbox

I have made a simple package for those who just want, like me, a working out of the box ScrollView, without having to apply all the time the styles to make it work properly.

https://github.com/SrBrahma/pagescrollview

Usage:

import { PageScrollView } from 'pagescrollview';

<PageScrollView>
  {/** Your contents */}
</PageScrollView>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionagmcleodView Question on Stackoverflow
Solution 1 - FlexboxRaphael NjengaView Answer on Stackoverflow
Solution 2 - FlexboxMalte Schulze-BoeingView Answer on Stackoverflow
Solution 3 - FlexboxFellow StrangerView Answer on Stackoverflow
Solution 4 - FlexboxStanoView Answer on Stackoverflow
Solution 5 - FlexboxElvis S.View Answer on Stackoverflow
Solution 6 - FlexboxAlbert Vila CalvoView Answer on Stackoverflow
Solution 7 - FlexboxHenrique BrunoView Answer on Stackoverflow