React Native – How to make image width 100 percent and vertical top?

The image is vertically centered, because you added flex: 1 to the style property. Don’t add flex: 1, because that will fill the image to its parent, which is not desired in this case.

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 16×9, the height is 9/16th of the width of the image. The width equals device width, so:

const dimensions = Dimensions.get('window');
const imageHeight = Math.round(dimensions.width * 9 / 16);
const imageWidth = dimensions.width;

return (
   <Image
     style={{ height: imageHeight, width: imageWidth }}
   />
);

Note: When using an implementation like this, your image will not automatically resize when rotating your device, using split screen, etc. You will have to take care of those actions as well if you support multiple orientations…

In case the ratio is not the same, dynamically change the 9 / 16 by the ratio for each different image. If you don’t really bother the image is a little bit cropped, you can use cover mode with a fixed height as well: (https://snack.expo.io/rk_NRnhHb)

<Image
  resizeMode={'cover'}
  style={{ width: '100%', height: 200 }}
  source={{uri: temp}}
/>

Leave a Comment

techhipbettruvabetnorabahisbahis forumuedueduseduseduedusedueduedusedusedu