如何在官方 react-native Modal 上添加背景可点击叠加层?
IT技术
javascript
reactjs
react-native
2021-05-11 11:53:48
3个回答
您可以将 Modal 内容包装在可触摸的不透明度中并使用背景对其进行样式设置。我编辑了文档中给出的示例。
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}>
<TouchableOpacity
style={{ backgroundColor: 'black', flex: 1,justifyContent:'center' }}
onPress={() => setModalVisible(!modalVisible)}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<TouchableHighlight
style={{ ...styles.openButton, backgroundColor: '#2196F3' }}
onPress={() => {
setModalVisible(!modalVisible);
}}>
<Text style={styles.textStyle}>Hide Modal</Text>
</TouchableHighlight>
</View>
</TouchableOpacity>
</Modal>
React Native Elements 有一个很棒的组件Overlay
,可以解决您的问题。本周早些时候它救了我。唯一的问题是它是更大图书馆的一部分。
https://react-native-elements.github.io/react-native-elements/docs/overlay.html
我认为如果背景和模态之间的分离对您的项目并不重要,您可以这样处理:
<Modal
animationType="slide"
transparent={true}
style={{ width: '100%', alignSelf: 'center', height: '100%', justifyContent: 'flex-start' }}
visible={this.state.modalVisible}
onRequestClose={() => {
// Do smth
}}>
<TouchableWithoutFeedback style={{ flex: 1 }} onPress={() => {
// Do the backdrop action
}}>
<View style={{ backgroundColor: '#fff', flex: 1, width: '80%', height: '50%' }} >
<Text> Your content </Text>
</View>
</TouchableWithoutFeedback>
</Modal>
其它你可能感兴趣的问题