如何在React导航(3.x)中为全屏模式提供透明背景?

时间:2018-11-22 09:46:01

标签: react-native react-navigation react-navigation-stack

如何在反应导航(3.x)中为全屏模式提供透明背景。给定here的解决方案在新版本的反应导航中不起作用。 我想要一种新的全屏模式中的透明颜色,该颜色将显示在另一个屏幕上。

  const MainNavigator = createStackNavigator(
  {
    BrowserHome: { screen: BrowserHome },
    ImageDetailModal: {
      screen: ImageDetail,
      navigationOptions: {
        header: null
      }
    }
  },
  {
    mode: "modal",
    cardStyle: {
      backgroundColor: "transparent",
      opacity: 1
    }
  }
);

const AppContainer = createAppContainer(MainNavigator);

export default AppContainer;

在“ BrowserHome”组件上显示的“图像细节”组件为:

export default class ImageDetail extends React.Component {
  render() {
    const modalColor = this.props.navigation.getParam("modalColor");
    return (
      <View
        style={{ flex: 1, flexDirection: "column", justifyContent: "flex-end" }}
      >
        <View
          style={{
            height: "50%",
            width: "100%",
            backgroundColor: "red",
            justifyContent: "center"
          }}
        >
          <Text>Testing a modal with transparent background</Text>
        </View>
      </View>
    );
  }
}

3 个答案:

答案 0 :(得分:6)

React 导航 5 倍

如果您更新到 React Navigation 版本 5x,这里有一个更新。

代码示例

<Stack.Navigator
  screenOptions={{
    headerShown: false,
    cardStyle: { backgroundColor: 'transparent' },
    cardOverlayEnabled: true,
    cardStyleInterpolator: ({ current: { progress } }) => ({
      cardStyle: {
        opacity: progress.interpolate({
          inputRange: [0, 0.5, 0.9, 1],
          outputRange: [0, 0.25, 0.7, 1],
        }),
      },
      overlayStyle: {
        opacity: progress.interpolate({
          inputRange: [0, 1],
          outputRange: [0, 0.5],
          extrapolate: 'clamp',
        }),
      },
    }),
  }}
  mode="modal"
>
  <Stack.Screen name="Home" component={HomeStack} />
  <Stack.Screen name="Modal" component={ModalScreen} />
</Stack.Navigator>

此处的属性 cardOverlayEnabledheaderShown可选。这将取决于您要实现的目标。

答案 1 :(得分:2)

您现在可以删除cardStyle对象,而改为设置transparentCard: true

const MainNavigator = createStackNavigator(
  {
    BrowserHome: { screen: BrowserHome },
    ImageDetailModal: {
      screen: ImageDetail,
      navigationOptions: {
        header: null
      }
    }
  },
  {
    mode: "modal",
    transparentCard: true
  })

https://reactnavigation.org/docs/en/stack-navigator.html#stacknavigatorconfig

答案 2 :(得分:-1)

在createStackNavigator中添加行transparentCard: truecardStyle: {opacity: 1}

const MainStack = createStackNavigator({
  Tabs: TabNavigator,
  Modal: ModalScreen, 
},{
  headerMode: 'none',
  mode: 'modal',
  transparentCard: true,
  cardStyle: { opacity: 1 }
});