I am trying to display list of images in a scrollview. Width should be 100%, while height should be automatic, keeping aspect ratio.
The searches I did pointed to various solutions which give fullscreen background style.
const styles = StyleSheet.create({ image: { width: null, height: 300, resizeMode: 'cover' } }); <ScrollView style={{flex: 1}}> <Image style={styles.image} source={require('../../../images/collection-imag1.png')}/> <Image style={styles.image} source={require('../../../images/collection-imag2.png')}/> </ScrollView>
I have tried various combinations of width: null, height: null, flex: 1, alignSelf etc. The above solution is almost working, except the height is not dynamic. Parts of the image are not visible.
So all we need to do is create a View with width: "100%" , then use onLayout to get the width of that view (i.e. the container width), then use that container width to calculate the height of our image appropriately.
In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.
To fit an Image to the full width of the screen and maintain aspect ratio with React Native, we can set the width to null and the height to the height we want. to set the height of the Image to 300 and the width to null to fit the Image to make the width flexible and maintain aspect ratio.
You can get the size using Image.getSize static method from react-native You always have to set the width and height of an Image. It is not going to automatically size things for you. The React Native docs says so. You should measure the total height of the ScrollView using onLayout and set the height of the Image s based on it.
You should always add a height and width on an image in React Native. In case the image is always the same, you can use Dimensions.get ('window').width to calculate the size the image should be. For example, if the ratio is always 16x9, the height is 9/16th of the width of the image. The width equals device width, so:
To set image width to be 100% and height to be auto in React Native, we can set the width and height of the Image. to call Dimensions.get with 'window' to get the window’s dimension. Then we calculate the ratio between the width and height of the image with win.width / 200, where 200 is the width of the image.
Always remember, in react native, there is no way to work with remote images without providing height, width values. You need to know these values. There is also a way by which you can work with only one value of either width or height. In that case you will need the value of aspect ratio.
So after thinking for a while I was able to achieve height: auto in react-native image. You need to know the dimensions of your image for this hack to work. Just open your image in any image viewer and you will get the dimensions of the your image in file information. For reference the size of image I used is 541 x 362
First import Dimensions from react-native
import { Dimensions } from 'react-native';
then you have to get the dimensions of the window
const win = Dimensions.get('window');
Now calculate ratio as
const ratio = win.width/541; //541 is actual image width
now the add style to your image as
imageStyle: { width: win.width, height: 362 * ratio, //362 is actual height of image }
"resizeMode"
isn't style property. Should move to Image component's Props
like below code.
const win = Dimensions.get('window'); const styles = StyleSheet.create({ image: { flex: 1, alignSelf: 'stretch', width: win.width, height: win.height, } }); ... <Image style={styles.image} resizeMode={'contain'} /* <= changed */ source={require('../../../images/collection-imag2.png')} /> ...
Image's height won't become automatically because Image component is required both width and height in style props
. So you can calculate by using getSize() method for remote images like this answer and you can also calculate image ratio for static images like this answer.
There are a lot of useful open source libraries -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With