What's the best way to add a full screen background image
I wanted to add a full-screen image to the View so I write this code
return (
<View style={styles.container}>
<Image source={require('image!egg')} style={styles.backgroundImage}/>
</View>
)
and defined the style as
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
flexDirection: 'column',
},
backgroundImage:{
width:320,
height:480,
}
...
but in this way how am I supposed to find the actual iPhone screen size?
I've seen an API to access the Pixel Density but nothing about the screen size...
Solution 1:
(This has been deprecated now you can use ImageBackground)
This is how I've done it. The main deal was getting rid of the static fixed sizes.
class ReactStrap extends React.Component {
render() {
return (
<Image source={require('image!background')} style={styles.container}>
... Your Content ...
</Image>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
// remove width and height to override fixed static size
width: null,
height: null,
}
};
Solution 2:
You can use flex: 1
styling on an <Image>
element to have it fill the whole screen. You can then use one of the Image resize modes to have the image completely fill the element:
<Image source={require('image!egg')} style={styles.backgroundImage} />
Style:
import React from 'react-native';
let { StyleSheet } = React;
let styles = StyleSheet.create({
backgroundImage: {
flex: 1,
resizeMode: 'cover', // or 'stretch'
}
});
I'm pretty sure you can get rid of the <View>
wrapping your image and this will work.
Solution 3:
Note: This solution is old. Please refer to https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting instead
Try this solution. It is officially supported. I have just tested it and works flawlessly.
var styles = StyleSheet.create({
backgroundImage: {
flex: 1,
alignSelf: 'stretch',
width: null,
}
});
And as for using it as Background image, just do the following.
<Image style={styles.backgroundImage}>
<View>
<Text>All your stuff</Text>
</View>
</Image>