17

So I'm setting a background image in react native but I need the position of the background image to be set to bottom. So if anyone knows how to achieve this.

I have tried adding backgroundPosition but it seems it is not supported

<ImageBackground
  source={require("../assets/img/bg-top.png")}
  // resizeMethod={'auto'}
  style={{
    width: "100%",
    height: 120,
    padding: 20,
    paddingVertical: 40,
    backgroundPosition: "bottom" // not working
  }}
  imageStyle={{
    resizeMode: "cover",
    alignSelf: "flex-end"
  }}
>
  <Text style={{ color: "#D8D8D8", fontSize: 16 }}>Login</Text>
</ImageBackground>;

I expect the image alignment should start from the bottom rather it starts from the top

5 Answers 5

11

React native provided bottom and position tag to set UI at bottom. Please replace backgroundPosition tag from image style into

position: 'absolute', bottom:0

 <ImageBackground
  // resizeMethod={'auto'}
  style={{
    width: "100%",
    height: 120,
    backgroundColor:'#000',
    padding: 20,
    paddingVertical: 40,
    position: 'absolute',
  bottom:0
  }}
  imageStyle={{
    resizeMode: "cover",
    alignSelf: "flex-end"
  }}
>
  <Text style={{ color: "#D8D8D8", fontSize: 16 }}>Login</Text>
</ImageBackground>

Please check snack expo

https://snack.expo.io/@vishal7008/bottom-ui

Sign up to request clarification or add additional context in comments.

Comments

6

The image inside ImageBackground has the default style:

position: 'absolute'
top: 0
bottom: 0
right: 0
left: 0

So, we can just remove the top: 0 by setting

top: undefined

together we have

<ImageBackground
  source={require("../assets/img/bg-top.png")}
  // resizeMethod={'auto'}
  style={{
    width: "100%",
    height: 120,
    padding: 20,
    paddingVertical: 40,
    overflow: 'hidden' // prevent image overflow the container
  }}
  imageStyle={{
    resizeMode: "cover",
    height: 200, // the image height
    top: undefined
  }}
>
  <Text style={{ color: "#D8D8D8", fontSize: 16 }}>Login</Text>
</ImageBackground>

Comments

2

For implementing the position of the background image I use the following code, actually I use the ImageBackground component as the wrapper of the entire screen:

<ImageBackground
  style={styles.screenWrapper}
  imageStyle={styles.backgroundStyle}
  source={{ uri: movieDetail?.image?.medium?.url }}
>
 // children

~~~

const styles = createStyle({
  screenWrapper: {
    flex: 1,
    position: 'relative',
  },
  backgroundStyle: {
    resizeMode: 'cover',
    position: 'absolute',
    top: 0,
    bottom: '45%',
  },

Finally, I reach my desire:

enter image description here

Note: The above codes are not workable, it is just pseudo-code to convey my exact idea.

Comments

1

the backgroundPosition you know from CSS is not supported according the docs.

There is a similar question:

https://stackoverflow.com/a/53726116/9899193

Does this help you?

Comments

0

Using flexbox and percentages:

function () {
  const [height, setHeight] = useState(0);
  // or const {height} = useWindowDimensions();

  return (
    <View
      style={{flex: 1, justifyContent: 'flex-end'}}
      onLayout={e => setHeight(e.nativeEvent.layout.height)}>
      <ImageBackground
        style={{
          width: '100%',
          height: height * 0.75,
        }}
        alt="..."
        source={"..."}
      />
    </View>
  );
}

The trick is you need a hard number for height.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.