简体   繁体   中英

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!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM