【问题标题】:Set ImageBackground using Stack Navigator使用 Stack Navigator 设置 ImageBackground
【发布时间】:2021-11-23 08:18:30
【问题描述】:

我所面临的:

我有一个StackNavigator,我想在其中为它的所有子屏幕设置一个ImageBacgkround。问题是,我已经设置了我的 StackNavigator cardStyle: { backgroundColor: 'transparent' },跟在 React Navigation Docs 之后,并将其包裹在 ImageBackground 中,但仍然无法正常工作。

我的尝试:

//My navigator component is more complex, but the concept looks something like this
//for both navigators
const Stack = createStackNavigator();

const MyStackNavigator = () => (
  <ImageBackground source=('../assets/my-background.png') resizeMode='cover' style={{flex: 1}}>
    //If I change BackgroundColor to 'red' or anything like that, it works fine
    //so I think the problem isn't related to styling properties 
    <Stack.Navigator screenOptions={{cardStyle: { BackgroundColor: 'transparent' } }}>
      <Stack.Screen name='Screen1' component={Component1}/>
      <Stack.Screen name='Screen2' component={Component2}/>
      <Stack.Screen name='Screen3' component={Component3}/>
    </Stack.Navigator>
  </ImageBackground>
);

export default App() => (
  <MyStackNavigator />
);

我在导航器组件中的DrawerNavigator 中通过sceneContainerStyle: { backgroundColor: 'transparent' } }} 做了同样的事情,它确实有效。我的 StackNavigator 似乎没有正确渲染它的父组件。

我的 StackNavigator 的一个屏幕:

我的 DrawerNavigator 的一个屏幕:

两个导航器的屏幕布局都包裹在一个透明容器View 中,因此您可以看到,抽屉屏幕正在正确呈现背景,但堆栈屏幕却不是。也许我将错误的道具传递给我的 StackNavigator?

【问题讨论】:

    标签: javascript css reactjs react-native react-navigation


    【解决方案1】:

    使用 react-navigation 6,您可以使用主题选项自定义外观以使背景透明(默认为 rgb(242,242,242))使用 NavigatorContainer 应用主题

    here is an answer that may help

    【讨论】:

    • 感谢您的回答,但正如我在代码中所说,我可以将 backGroundColor 设置为任何颜色,甚至 rgba 值,所以我认为问题与样式无关。
    • 该链接包含带有 Imagebackground 的堆栈导航器的代码,您检查了吗?
    • 是的,我的代码与链接的代码非常相似,除了样式方法(这不是我的问题的原因)。
    • 您能否像这样测试为每个屏幕应用您的图像:&lt;Stack.Screen name='Home' component={HomeScreen} options={{ title: &lt;Image style={{ width: 250, height: 50 }} source = require('../images/yourimage.png')}/&gt; }} /&gt;。我的意思是分别为每个屏幕使用图像源,而不将堆栈包装在 &lt;ImageBackground 中。
    • @RANJANKUMARSINGH 再次测试了您的解决方案,它成功了!还从 (stackoverflow.com/a/64570748/16689004) 那里得到了一些帮助,但还是谢谢。
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 2021-01-21
    • 2020-06-14
    • 2020-02-06
    相关资源
    最近更新 更多