1

I am using ImageBackground in my react native app, but it not covering full height of the View

enter image description here

You can see that there is red space which is not covered by my image.

Below is my code:

<View
  style={styles.subDomainItem}>
  <ImageBackground
    resizeMode={'cover'}
    source={ImageConfig.script_default_cloud}
    style={{
      width: '100%',
      height: (dimensions.width - 20) / 2,
      backgroundColor: 'red',
      flex: 1,
      justifyContent: 'center',
    }}></ImageBackground>
</View>

subDomainItem: {
    maxWidth: '94%',
    width: '94%',
    marginHorizontal: '3%',
    marginVertical: 10,
    overflow: 'hidden',
    borderRadius: 10,
    flex: 1,
    elevation: 8,
  },

I want my image to cover all the red spaces at top and bottom, but I don't know what I am doing wrong.

pratteek shaurya
  • 850
  • 2
  • 12
  • 34

3 Answers3

0

You don't want to give a height and width for ImageBackground if you want to cover all space of parent View you just need give flex: 1 or height and width to 100% for ImageBackground.

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

const WIDTH = Dimensions.get('screen').width;
const HEIGHT = Dimensions.get('screen').height;

const MainPage = () => {
   return (
     <View style={styles.subDomainItem}>
        <ImageBackground
            resizeMode="contain"
            source={ImageConfig.script_default_cloud}
            style={styles.bgImageStyle}
        >
            <Text>Sample</Text>
      </ImageBackground>
   </View>
);
};

const styles = StyleSheet.create({
  subDomainItem: {
      width: WIDTH,
      height: HEIGHT,
      alignItems: 'center',
  },
  bgImageStyle: {
    justifyContent: 'center',
    resizeMode: 'contain',
    width: WIDTH,
    height: HEIGHT,
  }
})
Rahman Haroon
  • 1,088
  • 2
  • 12
  • 36
0

Try this. is wroking fine

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

const Backimage =  () => {
    return(
        <ImageBackground 
            style={{width:Dimensions.get("screen").width, height:Dimensions.get("screen").height}}
            source={require("./src/assets/water.png")} resizeMethod="resize">
            
        </ImageBackground>
    )
}

export default Backimage
Engr.Aftab Ufaq
  • 3,356
  • 3
  • 21
  • 47
0

I think you should give your subDomainItem view an explicit height when styling it. You gave it a width of 94% but you haven‘t specified a height.

ImageBackground should then work properly by giving it a height of 100% if the parent component (subDomainItem in this case) has an explicit height.

smbrmoyo
  • 61
  • 3