ReactNative之自定義導(dǎo)航條

前言

眼看很多公司都開始嘗試使用ReactNative,達(dá)到跨平臺(tái)開發(fā),最近也寫了很多文章,希望讓更多想了解的同學(xué)快速上手ReactNative.

如果喜歡我的文章,可以關(guān)注我微博:袁崢Seemygo

ReactNative之自定義導(dǎo)航條

  • 用RN開發(fā)App,肯定需要用到導(dǎo)航條,系統(tǒng)自帶的導(dǎo)航條不好用,一般都需要自定義導(dǎo)航條。
  • 自定義導(dǎo)航條思想:模仿iOS導(dǎo)航控制器封裝了一套R(shí)N的導(dǎo)航條
  • 自定義導(dǎo)航條暴露屬性
    • 導(dǎo)航條展示什么控件,全部由外界傳進(jìn)來,這樣設(shè)計(jì),拓展性比較好
 static propTypes = {
        // 左邊按鈕
        leftBarButtonItem:PropTypes.object,
        // 中間View
        middleView:PropTypes.object,
        // 右邊按鈕
        rightBarButtonItem:PropTypes.object,
        // 中間標(biāo)題
        title:PropTypes.string,
        // 中間標(biāo)題樣式
        titleStyle:PropTypes.object,
        // 導(dǎo)航條整個(gè)內(nèi)容,完全由自己自定義
        contentView:PropTypes.object
    };

自定義導(dǎo)航條封裝代碼

/**
 * Created by ithinkeryz on 2017/5/15.
 */

import React, { Component,PropTypes } from 'react';

import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Dimensions
} from 'react-native';

import Common from './Common'

const NavigatorBarHeight = 64;

export default class CommonNavigationBar extends Component {

    // 暴露屬性
    static propTypes = {
        // 左邊按鈕
        leftBarButtonItem:PropTypes.object,
        // 中間View
        middleView:PropTypes.object,
        // 右邊按鈕
        rightBarButtonItem:PropTypes.object,
        // 中間標(biāo)題
        title:PropTypes.string,
        // 中間標(biāo)題樣式
        titleStyle:PropTypes.object,
        // 導(dǎo)航條整個(gè)內(nèi)容,完全由自己自定義
        contentView:PropTypes.object
    };

    constructor(props){
        super(props);

        // 不能同時(shí)設(shè)置中間標(biāo)題和中間View
        if (this.props.title && this.props.middleView)  throw "導(dǎo)航控制器不能同時(shí)設(shè)置title,middleView"

        // 設(shè)置了contentView,不要同時(shí)設(shè)置其他屬性
        if (this.props.contentView && (this.props.middleView ||
            this.props.rightBarButtonItem || this.props.title  ||
            this.props.titleStyle || this.props.contentView)
        ) throw "設(shè)置了contentView,其他設(shè)置無效,不要同時(shí)設(shè)置"

    }

    // 渲染內(nèi)容層
    renderContentView(){
        return (
            <View style={styles.contentViewStyle}>
                {/*左邊*/}
                <View style={styles.leftStyle}>
                    {this.props.leftBarButtonItem}
                </View>

                {/*中間*/}
                <View style={styles.middleStyle}>
                    {this.props.title?this.renderMiddleTitle():this.props.middleView}
                </View>

                {/*右邊*/}
                <View style={styles.rightStyle}>
                    {this.props.rightBarButtonItem}
                </View>
            </View>
        )
    }

    // 渲染中間標(biāo)題
    renderMiddleTitle(){
        return <Text style={[styles.middleTitleStyle,this.props.titleStyle]}>{this.props.title}</Text>
    }

    render() {
        return (
            <View style={[styles.barStyle,this.props.barStyle]}>
                {this.props.contentView?this.props.contentView:this.renderContentView()}
            </View>
        );
    }
}

var styles = StyleSheet.create({
    barStyle:{
        backgroundColor:'white',
        width:Common.screenW,
        height:NavigatorBarHeight,
        flexDirection:'row'
    },
    contentViewStyle:{
        flexDirection:'row',
        width:Common.screenW,
        height:44,
        backgroundColor:'white',
        position:'absolute',
        bottom:0
    },
    leftStyle:{
        flex:1,
        justifyContent:'center',
        alignItems:'center'
    },
    middleStyle:{
        flex:4,
        justifyContent:'center',
        alignItems:'center'
    },
    rightStyle:{
        flex:1,
        justifyContent:'center',
        alignItems:'center'
    },
    middleTitleStyle:{
        fontSize:20,
        color:'black',
        fontWeight:'bold'
    }
});

自定義導(dǎo)航條使用

  • 這樣封裝,使用非常簡(jiǎn)單
  • 封裝思想:易用,可擴(kuò)展,兩個(gè)條件都達(dá)到了
export default class Reading extends Component {

    render() {
        return (
            <View style={styles.viewStyle}>
                <CommonNavigationBar middleView={this.renderMiddleView()}
                                     leftBarButtonItem={this.renderLeftBarButtonItem()}
                                     rightBarButtonItem={this.renderRightBarButtonItem()}
                />

            </View>
        );
    }


    renderMiddleView(){
        return (
            <View>
                <Text>微信</Text>
            </View>
        )
    }

    renderLeftBarButtonItem(){
        return (
            <TouchableOpacity>
                <Image source={{uri:'nav_item_game_click_icon'}} style={{width:20,height:20}}/>
            </TouchableOpacity>
        )
    }
    renderRightBarButtonItem(){
        return (
            <TouchableOpacity>
                <Text>右邊</Text>
            </TouchableOpacity>
        )
    }
}
  • 效果
自定義導(dǎo)航條.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,113評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,410評(píng)論 4 61
  • 曾經(jīng)看到過一篇文章,開頭一句話:想要入門編程,先讀完這 100 本書吧,然后下面是一張超長(zhǎng)書單。當(dāng)時(shí)對(duì)著這張書單,...
    rick_ast閱讀 4,039評(píng)論 0 7
  • 雙十一大家剁手的都很歡樂,這次給吃土少女們推薦的app都是免費(fèi)的,但是由于使用頻率非常高,也有剁手之義,奉行l(wèi)e...
    時(shí)小穎閱讀 825評(píng)論 0 9
  • 前言:這應(yīng)該是最后一章了,故事雖然到這就結(jié)束了,但現(xiàn)實(shí)里還要繼續(xù)下去,希望在很久的以后來回顧時(shí),能因自己學(xué)生時(shí)代有...
    請(qǐng)叫我大蘇閱讀 650評(píng)論 1 2

友情鏈接更多精彩內(nèi)容