Even though I can get a screen orientation change to update state and re-render, the ImageBackground component still will not update its width.
I have the following code:
<View
onLayout={event => this.mylayoutchange(event)}
style={{ flex: 1, backgroundColor: 'green' }}
>
<ImageBackground
style={{ flex: 1, width: this.state.width, height: this.state.height }}
imageStyle={{ resizeMode: 'repeat' }}
source={require('../assets/images/my_background.jpg')}
>
<View>
<Text>.... Other code here....</Text>
</View>
</ImageBackground>
</View>;
When the user changes the orientation of the device the mylayoutchange() function gets called. It updates state correctly. The render function will update. Width and height are correctly changed as can be seen in console.log()
. However, for whatever reason, the <ImageBackground>
does not update its correct width and height.
When the screen is rotated, the background image no longer fills the entire size of the screen and I instead see the green background color.
What am I doing wrong?
My environment:
"react": "16.13.1",
"react-native": "0.63.2",
You need to use
resize
forresizeMethod
in order for the image to get actual resize:It's obvious that RN picks
scale
in your case, thus you have to explicitly set it toresize
for it to work when the orientation changes and flex style change kicks in:Result screen capture:
Tested with Environment:
Sample tile image used for repeat background (400 X 400 pixels):
Expo Snack:
RN ImageBackground Orientation Change