React-Native 組件之 Modal的使用詳解,react-nativemodal

來源:互聯網
上載者:User

React-Native 組件之 Modal的使用詳解,react-nativemodal

Modal組件可以用來覆蓋包含React Native根視圖的原生視圖(如UIViewController,Activity),用它可以實現遮罩的效果。

屬性

Modal提供的屬性有:

animationType(動畫類型) PropTypes.oneOf([‘none', ‘slide', ‘fade']

  • none:沒有動畫
  • slide:從底部滑入
  • fade:淡入視野

onRequestClose(被銷毀時會調用此函數)

在 ‘Android' 平台,必需調用此函數

onShow(模態顯示的時候被調用)

transparent (透明度) bool

為true時,使用透明背景渲染模態。

visible(可見度) bool

onOrientationChange(方向改變時調用)

在模態方向變化時調用,提供的方向只是 ” 或 ”。在初始化渲染的時候也會調用,但是不考慮當前方向。

supportedOrientations(允許模態旋轉到任何指定取向)[‘portrait', ‘portrait-upside-down', ‘landscape','landscape-left','landscape-right'])

在iOS上,模態仍然受 info.plist 中的 UISupportedInterfaceOrientations欄位中指定的限制。

樣本

Modal的使用非常簡單,例如:

<Modal animationType='slide'      // 從底部滑入  transparent={false}       // 不透明 visible={this.state.isModal}  // 根據isModal決定是否顯示 onRequestClose={() => {this.onRequestClose()}} // android必須實現 >

綜合例子:

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);

 運行效果:

從 modal 的源碼可以看出,modal 其實就是使用了 絕對位置,所以當 modal 無法滿足我們的需求的時候,我們就可以通過 絕對位置 自己來封裝一個 modal

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.