0

I am trying to vertically align Content component from NativeBase with ability to scroll if content inside is too big for the screen size.

This means that in large devices content is vertically aligned to center while in small devices is scrollable as its height is higher then device height.

Can achieve both in separate ways, but never together.

Components:

<Container style={styles.container}>
    <Content contentContainerStyle={styles.content}>
        {bunchOfContent}
    </Content>
</Container>

Styling for vertically aligned but not scrollable:

container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  content: {
    flex: 1,
    justifyContent: 'center'
  }

And for scrollable but not vertically aligned:

container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  content: {
    justifyContent: 'center'
  }
Brian
  • 3,850
  • 3
  • 21
  • 37
matejs
  • 163
  • 3
  • 17

1 Answers1

1

In react-native flex doesn't work the same way as it does in CSS. See the docs. Use flexGrow instead like the following style:

container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
},
content: {
    flexGrow: 1,
    justifyContent: 'center'
}

Use flexGrow: 1 instead of flex: 1 in your first content style.

The differences are still not clear to me but you can follow following thread:

  1. https://github.com/facebook/react-native/issues/11565
  2. flex vs flexGrow vs flexShrink vs flexBasis in React Native?

I will update this answer after understanding the differences properly.

Ankit Aggarwal
  • 2,941
  • 2
  • 17
  • 26