React Native學(xué)習(xí)教程之Modal控件自定義彈出View詳解
前言
最近在學(xué)習(xí)RN,好多知識(shí)都懶得寫,趁今天有空,來一發(fā)吧,Modal控件的一個(gè)小demo;下面話不多說了,來一起看看詳細(xì)的介紹吧。
參考文章地址:http://reactnative.cn/docs/0.27/modal.html#content
Modal組件可以用來覆蓋包含React Native根視圖的原生視圖(如UIViewController,Activity)。
在嵌入React Native的混合應(yīng)用中可以使用Modal。Modal可以使你應(yīng)用中RN編寫的那部分內(nèi)容覆蓋在原生視圖上顯示。
下面是代碼:
// HomePage
// 功能: 該類的作用
// Created by 小廣 on 2016-06-12 上午.
// Copyright © 2016年 All rights reserved.
'use strict';
import React, { Component } from 'react';
import {
View,
Text,
Image,
Modal,
Navigator,
TextInput,
ScrollView,
StyleSheet,
Dimensions,
TouchableHighlight,
} from 'react-native';
import NavigatorBar from '../tools/navigator'
var { width, height, scale } = Dimensions.get('window');
// 類
export default class HomePage extends Component {
// 構(gòu)造函數(shù)
constructor(props) {
super(props);
this.state = {
show:false,
};
}
// 加載完成
componentDidMount(){
//
}
// view卸載
componentWillUnmount(){
//
}
// 自定義方法區(qū)域
// your method
_leftButtonClick() {
}
_rightButtonClick() {
//
console.log('右側(cè)按鈕點(diǎn)擊了');
this._setModalVisible();
}
// 顯示/隱藏 modal
_setModalVisible() {
let isShow = this.state.show;
this.setState({
show:!isShow,
});
}
// 繪制View
render() {
return (
<View style={styles.container}>
<NavigatorBar
title='Modal測試'
titleTextColor='#F2380A'
rightItemTitle='按鈕'
rightTextColor='#F2380A'
rightItemFunc={this._rightButtonClick.bind(this)} />
<Modal
animationType='slide'
transparent={true}
visible={this.state.show}
onShow={() => {}}
onRequestClose={() => {}} >
<View style={styles.modalStyle}>
<View style={styles.subView}>
<Text style={styles.titleText}>
提示
</Text>
<Text style={styles.contentText}>
Modal顯示的View 多行了超出一行了會(huì)怎么顯示,就像這樣顯示了很多內(nèi)容該怎么顯示,看看效果
</Text>
<View style={styles.horizontalLine} />
<View style={styles.buttonView}>
<TouchableHighlight underlayColor='transparent'
style={styles.buttonStyle}
onPress={this._setModalVisible.bind(this)}>
<Text style={styles.buttonText}>
取消
</Text>
</TouchableHighlight>
<View style={styles.verticalLine} />
<TouchableHighlight underlayColor='transparent'
style={styles.buttonStyle}
onPress={this._setModalVisible.bind(this)}>
<Text style={styles.buttonText}>
確定
</Text>
</TouchableHighlight>
</View>
</View>
</View>
</Modal>
</View>
);
}
}
// Modal屬性
// 1.animationType bool 控制是否帶有動(dòng)畫效果
// 2.onRequestClose Platform.OS==='android'? PropTypes.func.isRequired : PropTypes.func
// 3.onShow function方法
// 4.transparent bool 控制是否帶有透明效果
// 5.visible bool 控制是否顯示
// css樣式
var styles = StyleSheet.create({
container:{
flex:1,
backgroundColor: '#ECECF0',
},
// modal的樣式
modalStyle: {
// backgroundColor:'#ccc',
alignItems: 'center',
justifyContent:'center',
flex:1,
},
// modal上子View的樣式
subView:{
marginLeft:60,
marginRight:60,
backgroundColor:'#fff',
alignSelf: 'stretch',
justifyContent:'center',
borderRadius: 10,
borderWidth: 0.5,
borderColor:'#ccc',
},
// 標(biāo)題
titleText:{
marginTop:10,
marginBottom:5,
fontSize:16,
fontWeight:'bold',
textAlign:'center',
},
// 內(nèi)容
contentText:{
margin:8,
fontSize:14,
textAlign:'center',
},
// 水平的分割線
horizontalLine:{
marginTop:5,
height:0.5,
backgroundColor:'#ccc',
},
// 按鈕
buttonView:{
flexDirection: 'row',
alignItems: 'center',
},
buttonStyle:{
flex:1,
height:44,
alignItems: 'center',
justifyContent:'center',
},
// 豎直的分割線
verticalLine:{
width:0.5,
height:44,
backgroundColor:'#ccc',
},
buttonText:{
fontSize:16,
color:'#3393F2',
textAlign:'center',
},
});
注意:NavigatorBar是我自定義的一個(gè)View,充當(dāng)導(dǎo)航條,你可以將其換成一個(gè)按鈕就行了;
效果如圖:

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互
有了JSPatch,我們便可以在iOS App開發(fā)中令JavaScript代碼調(diào)用原生的Objective-C屬性和方法等,下面就來詳細(xì)看一下如何在iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互2016-06-06
iOS應(yīng)用開發(fā)中矢量圖的使用及修改矢量圖顏色的方法
這篇文章主要介紹了iOS應(yīng)用開發(fā)中矢量圖的使用及修改矢量圖顏色的方法,文中的方法是在Adobe Illustrator中繪制矢量圖然后導(dǎo)入Xcode中使用,需要的朋友可以參考下2016-03-03
iOS字體抖動(dòng)動(dòng)畫的實(shí)現(xiàn)代碼
這篇文章主要介紹了iOS字體抖動(dòng)動(dòng)畫的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
簡單介紹iOS開發(fā)中關(guān)于category的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中關(guān)于category的應(yīng)用,代碼仍然基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09
詳解iOS App開發(fā)中UIViewController的loadView方法使用
這篇文章主要介紹了詳解iOS App開發(fā)中UIViewController的loadView方法使用,講解了訪問view屬性時(shí)loadView方法的調(diào)用及使用loadView時(shí)的一些注意點(diǎn),需要的朋友可以參考下2016-03-03
iOS狀態(tài)欄frame計(jì)算問題的實(shí)現(xiàn)
這篇文章主要介紹了iOS狀態(tài)欄frame計(jì)算問題的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

