Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native ScrollView items with equal padding on both top and bottom

I am designing a scroll component to looks like a gallery of images. I am trying to center the images based on the device height with equivalent padding on both top and bottom. But based on the devices the top padding is not looking good. I need help to make it consistent in all the devices.

To get Height and Width of the device i am using Dimension

  const {width: screenWidth, height: screenHeight} = 
   Dimensions.get('window');
    const width = screenWidth ;
    const height = screenHeight;

<View style={styles.DefaultView}>
          <ScrollView
           maximumZoomScale={1}
           minimumZoomScale={1}
           bouncesZoom={true}
           style={styles.scrollView}
           automaticallyAdjustContentInsets={true}
           pagingEnabled={true}
           horizontal={true}
           showsHorizontalScrollIndicator={false}
           onScroll={this._onScroll}
           contentContainerStyle={{flexGrow : 1, justifyContent : 'center'}}
           >
           {images.map((row, i) => (
              <View key={i}>
                  <Image
                      resizeMode='contain'
                      style={{width, height}}
                      source={{uri: row.src}}
                  />
              </View>
          ))}
           </ScrollView>
        </View>

const styles = StyleSheet.create({
    scrollView: {
        flex: 1,
        flexDirection: 'row',
    },
    DefaultView: {
        flex: 1,
        backgroundColor: '#000',
    },
});

Please refer attached image for the current output Iphone 6 plus

like image 777
Jothi Kannan Avatar asked Jun 06 '26 07:06

Jothi Kannan


1 Answers

there is a prop for this called contentContainerStyle

<ScrollView 
  contentContainerStyle={{
    paddingTop: 25,
    paddingBottom: 25,
  }}>
 *** CONTENT ***
</ScrollView>
like image 198
Davide Carpini Avatar answered Jun 08 '26 00:06

Davide Carpini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!