There are two approaches for this. One is to put everything in between the opening and closing <Image>
tags. The other is using layout properties. Refer to this link: https://medium.com/reactnative/background-images-in-react-native-191f3ed95a45
The first way is to use <Image>
as a container. Place your content between <Image>
and </Image>
. Be sure to set the content's backgroundColor: 'transparent'
. I think Android gives it by default but iPhone doesn't. So, for consistency, we need to explicitly state it. React Native warns you for doing it this way. It will soon become an error. So, I recommend the latter way.
const remote = 'https://s15.postimg.org/tw2qkvmcb/400px.png';
export default class BackgroundImage extends Component {
render() {
const resizeMode = 'center';
const text = 'This is some text inlaid in an <Image />';
return (
<Image
style={{
backgroundColor: '#ccc',
flex: 1,
resizeMode,
position: 'absolute',
width: '100%',
height: '100%',
justifyContent: 'center',
}}
source={{ uri: remote }}
>
<Text
style={{
backgroundColor: 'transparent',
textAlign: 'center',
fontSize: 30,
padding: 40,
}}
>
{text}
</Text>
</Image>
);
}
}
The second way is to use layout properties. Take a <View>
in a container and set {position:'absolute', width: '100%', height: '100%'}
. In this <View>
insert the <Image>
and set flex: 1
. You may want to add resizeMode
too. Now write a sibling <View>
in the same container and set {flex: 1, backgroundColor: 'transparent'}
. In this sibling <View>
place your content. You may want to set opacity
for either the <Image>
or the sibling <View>
.
Here's the example:
const remote = 'https://s15.postimg.org/tw2qkvmcb/400px.png';
export default class BackgroundImage extends Component {
render() {
const resizeMode = 'center';
const text = 'I am some centered text';
return (
<View
style={{
flex: 1,
backgroundColor: '#eee',
}}
>
<View
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
}}
>
<Image
style={{
flex: 1,
resizeMode,
}}
source={{ uri: remote }}
/>
</View>
<View
style={{
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'center',
}}
>
<Text
style={{
textAlign: 'center',
fontSize: 40,
}}
>
{text}
</Text>
</View>
</View>
);
}
}