如何使用本机反应和反应导航设置背景图像?

Gil*_*mon 6 react-native react-navigation

我正在使用 React Native 和 React Navigation v3,并且我正在尝试为我的整个应用程序设置背景图像。但由于某种原因,图像没有显示。

如果我包装我的 Home 组件,背景图像将按预期显示,但如果我包装堆栈导航器,则背景为白色。我在网上搜索了解决方案,但似乎不起作用。

const AppNavigator = createAppContainer(
  createStackNavigator(
    {
      Home: {screen: Home},
      Vocabulary: {screen: Vocabulary},
      AddWord: {screen: AddWord},
    },
    {
      initialRouteName: 'Home',
      headerMode: 'none',
      cardStyle: {backgroundColor: 'transparent', shadowColor:'transparent'},
      transitionConfig: () => ({
        containerStyle: {
          backgroundColor: 'transparent',
        },
      }),
    },
 ),
);
const App = () => {
 return (
    <ImageBackground
      source={require('./src/drawable/background1.jpg')}
      style={{flex: 1}}
      resizeMode="cover">
      <Provider store={store()}>
        <AppNavigator />
      </Provider>
    </ImageBackground>
 );
};
export default App;
Run Code Online (Sandbox Code Playgroud)

现在我看到了该组件,但背景是白色的。

小智 14

这是react-navigation v6.x的解决方案

按照@Bizkrem Muhammad 的回答中的建议,设置Stack Navigator 的属性cardStyle: {backgroundColor: 'transparent'}我来说不起作用screenOptions

但是,在这个 Github 问题的帮助下,我找到了一个解决方案,为我们的每个屏幕设置默认背景颜色NavigatorContainer

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

const navTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    background: 'transparent',
  },
};
Run Code Online (Sandbox Code Playgroud)

然后用 包裹你的or Navigator,像这样:NavigationContainer<ImageBackground>

return (
    <ImageBackground source={{/* your desired uri */}}>
        <NavigationContainer theme={navTheme} >
            {/* ... */}
        </NavigationContainer>
    </ImageBackground>
)
Run Code Online (Sandbox Code Playgroud)


Gil*_*mon 1

找到了解决方案,有点疯狂,但只有当你拼写透明而不是透明时它才有效。 回答我的问题 不能相信错误的拼写是解决方案。