skip to Main Content

I have a React Native app and I need to put image on the very top of the screen and also with width from one side of the screen to the other. I have put my image (black rectangle) into the app as a png and when I used position absolute and top 0 and left 0 but iÍ have noticed that even tho the image is from one side of the screen to the other, its not on the top of the screen so I have put background color to it a discovered that there is a lot of space around the image even tho when I open the image, its not there since its cropped to the very edges of the actual image. My question is how do I remove this space to get the desired look of my app (image on the very top with full width)?

const SignIn = () => {
  return (
    <ImageBackground source={paperBackground} style={styles.backgroundImage}>
      <Image source={topFrame} resizeMode="contain" style={styles.topFrame} />
    </ImageBackground>
  );
};

export default SignIn;

const styles = StyleSheet.create({
  backgroundImage: {
    flex: 1,
    width: "100%",
    height: "100%",
  },
  topFrame: {
    width: "100%",
    resizeMode: "contain",
    backgroundColor: "red",
    position: "absolute",
    top: 0,
    left: 0,
  },
});

enter image description here

2

Answers


  1. the reason you have this, is because of the resizeMode prop on the image, basically when you use resizeMode='contain' it resizes your image to show it fully in the confined image dimensions,

    if you want your image to show without the red spaces you should pass resizeMode='cover', this will make sure your image fits exactly into the dimension of its container, so regardless of the height & width of the container the image would always fit into it without leaving any space.

    Login or Signup to reply.
  2. Try this Style:

    const styles = StyleSheet.create({
      backgroundImage: {
        flex: 1,
        width: "100%",
        height: "100%",
        alignItems: 'center',
        justifyContent: 'flex-start',
      },
      topFrame: {
        width: "100%",
        height: "auto", // Adjust height if necessary
        resizeMode: "cover", // Fill the width
        position: "absolute",
        top: 0,
        left: 0,
      },
    });
    

    You can checkwith resizeMode: "cover" or resizeMode: "stretch" depending on how you want the image to fit the screen.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search