3

如何在反应导航(3.x)中为全屏模式提供透明背景。此处给出的解决方案不适用于新版本的反应导航。我想要在另一个屏幕上呈现的新全屏模式中的透明颜色。

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

4 回答 4

12

您现在可以删除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

于 2019-01-03T10:38:48.713 回答
7

反应导航 5x

如果你更新到 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这里是可选的。这将取决于您要达到的目标。

于 2021-01-04T11:57:04.127 回答
4

反应导航 6x

设置screenOptions.presentation为“transparentModal”。

于 2021-09-03T06:57:24.263 回答
1

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

const MainStack = createStackNavigator({
  Tabs: TabNavigator,
  Modal: ModalScreen, 
},{
  headerMode: 'none',
  mode: 'modal',
  transparentCard: true,
  cardStyle: { opacity: 1 }
});
于 2019-07-29T15:13:13.293 回答