ImageBackground 没有环绕 stack.navigator 并且 stack.screens 不可见。 (@react-navigation/stack": "^5.10.0 ) React-Native

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

我有多个具有相同背景的屏幕。我想在导航文件本身中包含 ImageBackground。但是不知何故屏幕不可见。

这是代码

<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',
  },

样式也包括在内

这是行不通的。 Screen1不显示,但背景正常显示。

我也尝试将导航器的 cardStyle 设为 backgroundColor:"transparent",甚至尝试 backgroundColor:"transperent" 但没有任何效果。

谢谢!!

您必须为此使用主题并将背景设置为透明。

导入默认主题

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

更新背景颜色

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

然后像这样包装导航容器。

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>
  );
}

您可以在此处查看示例小吃 https://snack.expo.io/@guruparan/createstacknavigator-|-react-navigation