1

I would like to create a horizontal ScrollView, but I faced some issues. The ScrollView takes all the remaining space of my screen, but what I want is the ScrollView to only take the height of the biggest children. How can I achieve that?

I have tried to set a fixed height on the style, but that didn't work. Any help?

<ScrollView horizontal showsHorizontalScrollIndicator={false} style={styles.guessList}>
{guessList.map((guess, index) => (
    <NumberContainer style={styles.guessItem} key={guess}>#{guessList.length - index} {guess}</NumberContainer>
))}
const styles= StyleSheet.create({
guessList: {
    height: 70
},
guessItem: {
    marginRight: 5
}

});

yesIamFaded
  • 1,970
  • 2
  • 20
  • 45
Daniel Chan
  • 343
  • 5
  • 13

4 Answers4

8

I just tried to add {flexGrow: 0} on the style of ScrollView and now it works. It's only taking the space enough for cotaining the children components.

Daniel Chan
  • 343
  • 5
  • 13
0

Like @Vignesh say, this should do the work

<ScrollView contentContainerStyle={{flexGrow: 1} ... >
BloodyMonkey
  • 1,572
  • 1
  • 9
  • 15
0

Here's a trick for you: get the biggest size and set it to the scrollview

<ScrollView horizontal showsHorizontalScrollIndicator={false} style={{height: this.state.biggest_child_height}}>
    {guessList.map((guess, index) => (
        <NumberContainer onLayout={(event) => {
            if(event.nativeEvent.layout.height > this.state.biggest_child_height){
                this.setState({biggest_child_height: event.nativeEvent.layout.height})
            }
        }} style={styles.guessItem} key={guess}>#{guessList.length - index} {guess}</NumberContainer>
    ))}
</ScrollView>

Edit:

You obviously need to play with flex or set a height to render the component first time

BloodyMonkey
  • 1,572
  • 1
  • 9
  • 15
0

Try this:

<View style={{maxHeight: 400, minHeight: 100}}>
   <ScrollView>
     ...
   </ScrollView>
</View>

After that View will get height depending on height of ScrollView.

M.N.
  • 65
  • 8