ReactNative之自定义导航条

前言

眼看很多公司都开始尝试使用ReactNative,达到跨平台开发,最近也写了很多文章,希望让更多想了解的同学快速上手ReactNative.

如果喜欢我的文章,可以关注我微博:袁峥Seemygo

ReactNative之自定义导航条

  • 用RN开发App,肯定需要用到导航条,系统自带的导航条不好用,一般都需要自定义导航条。
  • 自定义导航条思想:模仿iOS导航控制器封装了一套RN的导航条
  • 自定义导航条暴露属性
    • 导航条展示什么控件,全部由外界传进来,这样设计,拓展性比较好
 static propTypes = {
        // 左边按钮
        leftBarButtonItem:PropTypes.object,
        // 中间View
        middleView:PropTypes.object,
        // 右边按钮
        rightBarButtonItem:PropTypes.object,
        // 中间标题
        title:PropTypes.string,
        // 中间标题样式
        titleStyle:PropTypes.object,
        // 导航条整个内容,完全由自己自定义
        contentView:PropTypes.object
    };

自定义导航条封装代码

/**
 * 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,
        // 中间标题
        title:PropTypes.string,
        // 中间标题样式
        titleStyle:PropTypes.object,
        // 导航条整个内容,完全由自己自定义
        contentView:PropTypes.object
    };

    constructor(props){
        super(props);

        // 不能同时设置中间标题和中间View
        if (this.props.title && this.props.middleView)  throw "导航控制器不能同时设置title,middleView"

        // 设置了contentView,不要同时设置其他属性
        if (this.props.contentView && (this.props.middleView ||
            this.props.rightBarButtonItem || this.props.title  ||
            this.props.titleStyle || this.props.contentView)
        ) throw "设置了contentView,其他设置无效,不要同时设置"

    }

    // 渲染内容层
    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>
        )
    }

    // 渲染中间标题
    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'
    }
});

自定义导航条使用

  • 这样封装,使用非常简单
  • 封装思想:易用,可扩展,两个条件都达到了
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>
        )
    }
}
  • 效果
自定义导航条.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,142评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,195评论 4 61
  • 曾经看到过一篇文章,开头一句话:想要入门编程,先读完这 100 本书吧,然后下面是一张超长书单。当时对着这张书单,...
    rick_ast阅读 9,380评论 0 7
  • 双十一大家剁手的都很欢乐,这次给吃土少女们推荐的app都是免费的,但是由于使用频率非常高,也有剁手之义,奉行le...
    时小颖阅读 4,068评论 0 9
  • 前言:这应该是最后一章了,故事虽然到这就结束了,但现实里还要继续下去,希望在很久的以后来回顾时,能因自己学生时代有...
    请叫我大苏阅读 3,663评论 1 2