Search code examples
reactjsreact-nativereact-navigationreact-navigation-stack

ImageBackground is not wrapping the stack.navigator and the stack.screens are not visible. (@react-navigation/stack": "^5.10.0 ) React-Native


I have multiple screens with the same background. I thought of having the ImageBackground in the Navigation file itself. But somehow the screens are not visible.

Here is the code

<ImageBackground style={styles.imageContainer} source={pic1}>
   <Stack.Navigator>
     <Stack.Screen name="screen1" component="Screen1" />
   <Stack.Navigator>
</ImageBackground>



    imageContainer: {
    flex: 1,
    width: width,
    height: height,
    alignItems: 'center',
    resizeMode: 'contain',
    flexDirection: 'column',
  },

Style also included

This is not working. Screen1 is not displayed but the background is properly displayed.

I also tried giving the cardStyle for the navigator as backgroundColor:"transparent", even tried backgroundColor:"transperent" but nothing is working.

Thank You!!


Solution

  • You will have to use themes for this and set the background as transparent.

    Import defaulttheme

    import { NavigationContainer, DefaultTheme } from '@react-navigation/native';
    

    Update background color

    const MyTheme = {
      ...DefaultTheme,
      colors: {
        ...DefaultTheme.colors,
        primary: 'rgb(255, 45, 85)',
        background: 'transparent',
      },
    };
    

    And wrap the navigation container like this.

    export default function App() {
      const image = { uri: 'https://reactjs.org/logo-og.png' };
      return (
        <ImageBackground
          source={image}
          style={{
            flex: 1,
            resizeMode: 'cover',
            justifyContent: 'center',
          }}>
          <NavigationContainer theme={MyTheme}>
            <MyStack />
          </NavigationContainer>
        </ImageBackground>
      );
    }
    

    You can check the sample snack here https://snack.expo.io/@guruparan/createstacknavigator-|-react-navigation