react-native titleBar

import React, {Component} from 'react';
import {
    Image,
    StyleSheet,
    Text,
    View,
    Dimensions,
    TouchableOpacity
} from 'react-native';
import PropType from "prop-types";

/**
 * Created by yunpeng on
 * Desc:
 */
export default class TitleBar extends Component {
    constructor(props) {
        super(props)
    }
    //设置参数类型
    static propTypes = {
        //左边按钮,不设置就不显示
        leftIco: PropType.PropTypes.number,
        //右边按钮
        rightIco: PropType.PropTypes.number,
        //右边标题
        rightTitle: PropType.PropTypes.string,
        //中间标题
        titleText: PropType.PropTypes.string,
        //左边点击事件
        leftIcoPress: PropType.PropTypes.func,
        //右边点击事件
        rightIcoPress: PropType.PropTypes.func,
        //传入navigation用于返回事件
        navigation:PropType.PropTypes.object,
    };
    static defaultProps = {
        titleText: "这是标题",
    };

    render() {
        return (
            
                
                    {this._renderLeft()}
                
                    //设置文本缩略格式 tail clip
                        ellipsizeMode="tail"
                        //文本行数限制
                        numberOfLines={1}
                        style={style.centerTitleStyle} onPress={() => {
                    }}>{this.props.titleText}
                
                    {this._returnRight()}
                
            

        )
    }

    /**
     * 左边返回按钮
     * @returns {XML}
     * @private
     */
    _renderLeft(){
        if(!this.props.leftIco){

        } else if(this.props.navigation){
            return(
                this.props.leftIco}
                    onPress={()=>this.props.navigation.goBack()}
                />
            )
        }else if(this.props.leftIcoPress) {
            return(
                this.props.leftIco}
                    onPress={this.props.leftIcoPress}
                />
            )
        }

    }

    /**
     * 返回右边按钮
     * @returns {XML}
     * @private
     */
    _returnRight() {
        if (this.props.rightIco) {
            return this.props.rightIco} onPress={this.props.rightIcoPress}/>;
        } else if (this.props.rightTitle) {
            return //设置文本缩略格式 tail clip
                ellipsizeMode="tail"
                //文本行数限制
                numberOfLines={1}
                style={style.rightTitleStyle}>{this.props.rightTitle}
        }
    }
}

class ImageButton extends Component {
    render() {
        return (
            0.9} onPress={this.props.onPress}>
                {this._renderImg()}
            
        )
    }
    _renderImg(){
        if(this.props.defaultSource){
            return (
                this.props.style}
                    source={this.props.defaultSource}
                >
                    this.props.style}
                        source={this.props.source}
                    >
                        {this.props.children}
                    
                
            )
        }else {
            return (
                this.props.style}
                    source={this.props.source}
                >
                    {this.props.children}
                
            )
        }
    }
}

const style = StyleSheet.create({
    contentStyle: {
        backgroundColor: "#fff",
        flexDirection: "row",
        height: 40,
        alignItems: "center",
        paddingLeft: 10,
        paddingRight: 10,
        justifyContent: 'space-between',
    },
    imageStyle: {
        width: 16,
        height: 16,
    },
    centerTitleStyle: {
        position:"absolute",
        left:Dimensions.get("window").width/4,
        right:Dimensions.get("window").width/4,
        color: "#000",
        textAlign: "center",
        textAlignVertical: "center",
    },
    rightTitleStyle: {
        alignSelf: "flex-end",
        color: "#000",
        textAlignVertical: "center"
    }
});

react-native titleBar_第1张图片

你可能感兴趣的:(react-native)