skip to Main Content

I have one app in React Native and I’m trying to introduce an image in my app, but the image it’s to big and the image takes up the entire screen. To fix that, I tried

imagem:{
    flex: 1,
    width: null,
    height: null,
    resizeMode: 'contain'
  }

The image itself is at a good size, but the component it’s to larger, so it’s harder to see the other components.

The full script is here:

import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { SafeAreaView, StyleSheet, Text, View, Button, Image } from 'react-native';

export default function App() {  
  return (
    <SafeAreaView style={estilos.container}>
      <View>
        <Text>Cara ou Coroa</Text>
        <Image style={estilos.imagem} source={moedaAtual}/>
        <Button
          title='Girar'
          onPress={()=>{}}
        />
      </View>
    </SafeAreaView>
  );
}

const estilos = StyleSheet.create({
  container:{
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  imagem:{
    flex: 1,
    width: null,
    height: null,
    resizeMode: 'contain'
  }
});

2

Answers


  1. You can remove the flex: 1 property from the estilos.imagem

    const estilos = StyleSheet.create({
      container:{
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      },
      imagem:{
        width: null,
        height: null,
        resizeMode: 'contain'
      }
    });
    
    Login or Signup to reply.
  2. Set width to 100% and height to fixed size (to leave space for other components) and then either contain or cover for resizeMode to get the desired result.

    <Image
      style={{ width: '100%', height: 400, resizeMode: 'cover' }}
      source={{
        uri: 'https://picsum.photos/1920/1080',
      }}
    />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search