0

I am new with react native and I wanted to use ImageBackground.

However, when I try to make it display in full screen using the following code:

import React from 'react';
import { StyleSheet, ImageBackground,Text, View } from 'react-native';

const Signin = () => {
    return (
        <ImageBackground source={require('./images/background.jpg')} style={styles.image}> 
            <Text>Hello</Text>
        </ImageBackground>
            
    )
}


const styles = StyleSheet.create({
    image: {
        flex: 1,
        width: '100%',
        height: '100%',
        resizeMode:'cover'
    }
  });

export default Signin;

it returns only half of my screen the picture. Any ideas on why it is doing so?

enter image description here

My initial image properties are the follwing: enter image description here

My initial image can be found here: https://unsplash.com/photos/0AwoTNSdwV

colla
  • 717
  • 1
  • 10
  • 22
  • Does this answer your question? https://stackoverflow.com/questions/29322973/whats-the-best-way-to-add-a-full-screen-background-image-in-react-native/37705794 – Shubham Verma Aug 26 '20 at 09:59
  • Unfortunately no... I had a look at this answer before posting my question – colla Aug 26 '20 at 10:03

2 Answers2

0

I tried your code on my computer and the problem is with the image that you are using. Copy the following code and try it but this time download this picture and use it instead of the one you provided.

Picture link: https://drive.google.com/file/d/1brXApmH6b_t6pscY9jzt_TsJI9NglWGL/view?usp=sharing

Code:

import React from "react";
import { StyleSheet, ImageBackground, Text, View } from "react-native";

const Signin = () => {
  return (
    <ImageBackground
      source={require("./images/backgroundImage.jpg")}
      style={styles.image}
    >
      <Text>Hello</Text>
    </ImageBackground>
  );
};

const styles = StyleSheet.create({
  image: {
    flex: 1,
    width: "100%",
    height: "100%",
    resizeMode: "cover",
    justifyContent: "center",
    alignItems: "center",
  },
});

export default Signin;

This is what I get: enter image description here

You can also try different pictures as background but this should solve your problem

0

Try something like this.

Styles

var styles = StyleSheet.create({
container: {
    flex: 1,
},
backgroundImage: {
    flex: 1,
    resizeMode: 'cover', // or 'stretch'
},
loginForm: {
    position: 'absolute',
    top: 0,
    bottom: 0,
    left: 0,
    right: 0
},
});

View

    <View style={ styles.container }>
        <Image source={require('../images/login_background.png')} style=. {styles.backgroundImage}>
            <View style={ styles.loginForm }>
                <Text>TEST</Text>
            </View>
        </Image>
    </View>
Joan Louji
  • 19
  • 2
  • 4