【发布时间】:2018-08-06 04:08:29
【问题描述】:
如何设置React Navigation 模态视图的高度,以便它出现后从下往上只覆盖大约一半的屏幕,并且下面的视图仍然可见?
更新:我正在尝试创建一个类似于 App Store 购买模式的用户体验流程,其中某种 StackNavigator 嵌套在填充屏幕下半部分的模式中。 p>
【问题讨论】:
标签: react-native modal-dialog react-navigation
如何设置React Navigation 模态视图的高度,以便它出现后从下往上只覆盖大约一半的屏幕,并且下面的视图仍然可见?
更新:我正在尝试创建一个类似于 App Store 购买模式的用户体验流程,其中某种 StackNavigator 嵌套在填充屏幕下半部分的模式中。 p>
【问题讨论】:
标签: react-native modal-dialog react-navigation
在您的 stacknavigator 中,您可以设置以下选项:
mode: 'modal',
headerMode: 'none',
cardStyle:{
backgroundColor:"transparent",
opacity:0.99
}
在您的模态屏幕中:
class ModalScreen extends React.Component {
render() {
return (
<View style={{ flex: 1 ,flexDirection: 'column', justifyContent: 'flex-end'}}>
<View style={{ height: "50%" ,width: '100%', backgroundColor:"#fff", justifyContent:"center"}}>
<Text>Testing a modal with transparent background</Text>
</View>
</View>
);
}
}
你也可以参考我创建的这个博览会小吃https://snack.expo.io/@yannerio/modal来展示它是如何工作的,或者你可以使用React Native Modal
【讨论】:
opacity: 0.99是如何修复标准模态的透明度的吗?
transitionConfig 修复了 iOS 和 Andriod 中的透明度问题:) 仅供参考 opacity: 1 也可以使用
transparentCard: true 以使其与v3.x 一起工作,您可以在此处看到它工作snack.expo.io/@yannerio/ashamed-truffle
这是一个如何在react-navigation v3.x 中实现此目的的示例:
const TestRootStack = createStackNavigator(
{
TestRoot: TestRootScreen,
TestModal: {
screen: TestModalScreen,
navigationOptions: {
/**
* Distance from top to register swipe to dismiss modal gesture. Default (135)
* https://reactnavigation.org/docs/en/stack-navigator.html#gestureresponsedistance
*/
gestureResponseDistance: { vertical: 1000 }, // default is 135 },
},
},
},
{
headerMode: 'none',
mode: 'modal',
transparentCard: true,
},
);
const AppContainer = createAppContainer(TestRootStack);
class TestRootScreen extends React.Component {
render() {
return (
<SafeAreaView style={styles.container}>
<Button title="Show Modal" onPress={() => this.props.navigation.navigate('TestModal')} />
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'blue',
alignItems: 'center',
justifyContent: 'center',
},
});
class TestModalScreen extends React.Component {
render() {
return (
<SafeAreaView style={styles.container}>
<View style={styles.innerContainer} />
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'transparent',
justifyContent: 'flex-end',
},
innerContainer: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
top: 100,
backgroundColor: 'red',
},
});
【讨论】:
对于 react-navigation v3.x,你可以使用 prop transparentCard: true,你可以在这里查看更多细节:https://stackoverflow.com/a/55598127/6673414
【讨论】:
如果你想使用react native Modal,你可以让父视图透明并在底部添加一个视图
<Modal
animationType="slide"
transparent={true}
visible={props.visible}
>
<View
style={{
backgroundColor:'transparent',
flex:1,
justifyContent:'flex-end'
}}>
<View
style={{
backgroundColor:'green',
height:'20%'
}}>
<Text>Hello World!</Text>
<TouchableHighlight
onPress={props.closeModal}>
<Text>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
【讨论】: