React-Native 組件之 Modal的使用詳解
Modal組件可以用來覆蓋包含React Native根視圖的原生視圖(如UIViewController,Activity),用它可以實(shí)現(xiàn)遮罩的效果。
屬性
Modal提供的屬性有:
animationType(動(dòng)畫類型) PropTypes.oneOf([‘none', ‘slide', ‘fade']
- none:沒有動(dòng)畫
- slide:從底部滑入
- fade:淡入視野
onRequestClose(被銷毀時(shí)會(huì)調(diào)用此函數(shù))
在 ‘Android' 平臺(tái),必需調(diào)用此函數(shù)
onShow(模態(tài)顯示的時(shí)候被調(diào)用)
transparent (透明度) bool
為true時(shí),使用透明背景渲染模態(tài)。
visible(可見性) bool
onOrientationChange(方向改變時(shí)調(diào)用)
在模態(tài)方向變化時(shí)調(diào)用,提供的方向只是 ” 或 ”。在初始化渲染的時(shí)候也會(huì)調(diào)用,但是不考慮當(dāng)前方向。
supportedOrientations(允許模態(tài)旋轉(zhuǎn)到任何指定取向)[‘portrait', ‘portrait-upside-down', ‘landscape','landscape-left','landscape-right'])
在iOS上,模態(tài)仍然受 info.plist 中的 UISupportedInterfaceOrientations字段中指定的限制。
示例
Modal的使用非常簡(jiǎn)單,例如:
<Modal
animationType='slide' // 從底部滑入
transparent={false} // 不透明
visible={this.state.isModal} // 根據(jù)isModal決定是否顯示
onRequestClose={() => {this.onRequestClose()}} // android必須實(shí)現(xiàn)
>
綜合例子:
import React, { Component} from 'react';
import {
AppRegistry,
View,
Modal,
TouchableOpacity,
Text
} from 'react-native';
export default class ModalView extends Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
}
}
setModalVisible = (visible)=> {
this.setState({
modalVisible: visible
})
};
render(){
return(
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#ffaaff'}}>
<Modal animationType={'none'}
transparent={true}
visible={this.state.modalVisible}
onrequestclose={() => {alert("Modal has been closed.")}}
onShow={() => {alert("Modal has been open.")}}
supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
onOrientationChange={() => {alert("Modal has been OrientationChange.")}}>
<View style={{flex:1, marginTop: 22, backgroundColor: '#aaaaaa', justifyContent: 'center', alignItems: 'center'}}>
<View>
<Text>Hello World!</Text>
<TouchableOpacity onPress={() => {
this.setModalVisible(false)
}}>
<Text>隱藏 Modal</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
<TouchableOpacity onPress={() => {
this.setModalVisible(true)
}}>
<Text>顯示 Modal</Text>
</TouchableOpacity>
</View>
)
}
}
AppRegistry.registerComponent('ModalView', ()=>ModalView);
運(yùn)行效果:

從 modal 的源碼可以看出,modal 其實(shí)就是使用了 絕對(duì)定位,所以當(dāng) modal 無法滿足我們的需求的時(shí)候,我們就可以通過 絕對(duì)定位 自己來封裝一個(gè) modal
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React團(tuán)隊(duì)測(cè)試并發(fā)特性詳解
這篇文章主要為大家介紹了React團(tuán)隊(duì)測(cè)試并發(fā)特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
react自動(dòng)化構(gòu)建路由的實(shí)現(xiàn)
這篇文章主要介紹了react自動(dòng)化構(gòu)建路由的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
react如何使用useRef模仿抖音標(biāo)題里面添加標(biāo)簽內(nèi)容
本文介紹了如何模仿抖音發(fā)布頁面,使用div元素作為輸入框,并利用CSS樣式和JavaScript動(dòng)態(tài)操作DOM,實(shí)現(xiàn)類似于input輸入框的功能,感興趣的朋友跟隨小編一起看看吧2024-10-10
React+Redux實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList
這篇文章主要為大家詳細(xì)介紹了React+Redux實(shí)現(xiàn)簡(jiǎn)單的待辦事項(xiàng)列表ToDoList,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
antd?3.x?Table組件如何快速實(shí)現(xiàn)虛擬列表詳析
這篇文章主要給大家介紹了關(guān)于antd?3.x?Table組件如何快速實(shí)現(xiàn)虛擬列表的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用antd具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-11-11
React和Vue中實(shí)現(xiàn)錨點(diǎn)定位功能
在React中,可以使用useState和useEffect鉤子來實(shí)現(xiàn)錨點(diǎn)定位功能,在Vue中,可以使用指令來實(shí)現(xiàn)錨點(diǎn)定位功能,在React和Vue中實(shí)現(xiàn)錨點(diǎn)定位功能的方法略有不同,下面我將分別介紹,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

