如何在反应导航(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>
);
}
}
答案 0 :(得分:6)
如果您更新到 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>
此处的属性 cardOverlayEnabled
和 headerShown
是可选。这将取决于您要实现的目标。
答案 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: true
和cardStyle: {opacity: 1}
const MainStack = createStackNavigator({
Tabs: TabNavigator,
Modal: ModalScreen,
},{
headerMode: 'none',
mode: 'modal',
transparentCard: true,
cardStyle: { opacity: 1 }
});