反应导航默认背景颜色

Fac*_*cca 10 javascript android react-native react-navigation stack-navigator

我正在使用react-navigation和 stack-navigator 来管理我的屏幕。

我正在使用的平台:

  • 安卓
  • 反应本机:0.47.1
  • 反应导航:1.0.0-beta.11
  • 模拟器和设备

我有一个屏幕,它充当模态形式,但它实际上是一个全屏。为什么“作为模态形式”的部分很重要?那是因为它是一种带有一些选项和透明背景颜色的模态菜单。

这是我的期望:

在此处输入图片说明

但我得到的是:

在此处输入图片说明

如您所见,在第二个示例中,背景颜色被完全替换,或者之前加载的组件被卸载,因此我想要达到的效果丢失了。 这个想法是能够像任何其他屏幕一样导航到这个屏幕。

如果使用 react-navigation 无法完成,我可以采取什么其他方式来完成?

该组件执行操作(redux),因为它是一个跨应用程序组件并在内部封装了许多机制和逻辑,这就是为什么我不能将它用作PureComponent使用此组件的中继。至少,使这个组件成为 PureComponent 将迫使我在许多其他组件中复制许多机制和逻辑。

为了这个问题,也为了避免问题太大,两个屏幕的风格完全相同,但推动的一个StackNavigation取代了backgroundColor,或者卸载了以前的屏幕。

这是我到目前为止所拥有的:

//PlaylistSelector.js
render() {
  //Just a full size empty screen to check and avoid bugs
  //Using red instead of transparent, works

  return (
    <View style={{ flex: 1, backgroundColor: 'transparent' }}>
    </View>
  );
}

//Navigator.js
import { StackNavigator } from 'react-navigation';

import Album from './Album'; //This is the screen I expect to keep at the background
import PlaylistSelector from './PlaylistSelector';

const AppNavigator = StackNavigator(
    {
        ...moreScreens,
        Album: { screen: Album },
        PlaylistSelector: {
            screen: PlaylistSelector,
            navigationOptions: {
                style: { backgroundColor: 'red' } //Does not work, red is just to ilustrate, should be transparent,
                cardStyle: { //Does not work,
                    backgroundColor: 'transparent',
                },
                bodyStyle: { //Does not work,
                    backgroundColor: 'transparent',
                },
            }
        }
    },
    {
        initialRouteName: 'Splash',
        headerMode: 'none',
        cardStyle: { //Does not work
            backgroundColor: 'transparent',
        },
        transitionConfig: (): Object => ({ //Does not work
            containerStyle: {
                backgroundColor: 'transparent',
            },
        }),
    }
);

export default AppNavigator;
Run Code Online (Sandbox Code Playgroud)

Mug*_*gen 37

这在最新React Navigation版本中确实发生了变化。看

https://reactnavigation.org/docs/themes/

例如

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

const MyTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    background: 'red'
  },
};

export default function App() {
  return (
    <NavigationContainer theme={MyTheme}>{/* content */}</NavigationContainer>
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 我的应用程序使用深色,在屏幕转换期间出现白色闪烁,使用此功能消除了白色闪烁,谢谢。 (2认同)

小智 24

React-navigation v6.x中有一个解决方案

设置Stack Navigator 的 propcardStyle: {backgroundColor: 'transparent'}我来说不起作用screenOptions

但是,在Github 问题页面的帮助下,我找到了一个为 NavigatorContainer 设置默认背景颜色的解决方案:

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

...

const navTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    background: 'transparent',
  },
};

...

return (
    <NavigationContainer
      theme={navTheme}
...
Run Code Online (Sandbox Code Playgroud)

在那里我们可以改变'transparent'任何我们想要的。


Cri*_*ora 6

使用 react-navigation v3.x 可以使用 transparentCard pro:

const MainNavigator = createStackNavigator(
  {
    BottomTabs: {
      screen: BottomTabsStack,
    },
    Modal: {
      screen: ModalScreen,
    }
  },
  {
    headerMode: 'none',
    mode: 'modal',
    transparentCard: true,
    cardStyle: { opacity: 1 } //This prop is necessary for eventually issue.
  }
);
Run Code Online (Sandbox Code Playgroud)

您可以在下面找到完整的示例:

https://snack.expo.io/@cristiankmb/stacks-in-tabs-with-header-options-with-modal


小智 1

添加不透明度:

cardStyle: {
  backgroundColor: "transparent",
  opacity: 1
}
Run Code Online (Sandbox Code Playgroud)