• 使用自定义组件添加顶部导航栏


    源码

    /*
     * @Author: YooHoeh 
     * @Date: 2018-07-11 21:25:31 
     * @Last Modified by: YooHoeh
     * @Last Modified time: 2018-07-12 08:37:32
     */
    
    import React, { Component, PropTypes } from "react";
    import { View, Text, StyleSheet, Platform, StatusBar, } from "react-native";
    import { platform } from "os";
    
    const NAV_BAR_HEIGHT_ANDROID = 50;
    const NAV_BAR_HEIGHT_IOS = 44;
    // ios状态栏高度
    const STATUS_BAR_HEIGHT = 20;
    const statusBarShape={
      backgroundColor:PropTypes.string,
      barStyle:PropTypes.oneOf('default','light-content','dark-content'),
      hidden:PropTypes.bool,
    }  
    
    export default class NavigationBar extends Component {
      // 自定义组件并对其设置属性限定
      static propTypes = {
        style: View.propTypes.style,
        title: PropTyoes.string,
        titleView: PropTypes.element,
        hide: PropTypes.bool,
        leftButton: PropTypes.element,
        rightButton: PropTypes.element,
        statusBar:PropTypes.shap(statusBarShape)
      };
      // 组件默认设置
      static defaultProps={
        statusBar:{
          barStyle:'light-content',
          hidden:false,
        }
      }
      // 构造函数,默认导航栏不隐藏
      constructor(props) {
        super(props);
        this.state = {
          title: "",
          hide: false
        };
      }
      render() {
        // 状态栏
        let status = <View style={[styles.statusBar,this.props.statusBar]}>
          <StatusBar {...this.props.statusBar}/>
        </View>;
        // 判断标题是字符串还是一个view组件
        let titleView = this.props.titleView ? (
          this.props.titleView
        ) : (
          <Text style={styles.title}>{this.props.title}</Text>
        );
        let content = (
          <View style={styles.navBar}>
            {this.props.leftButton}
            <View style={styles.titleViewContainer}>{titleView}</View>
            {this.props.rightButton}
          </View>
        );
    
        return <View style={[StyleSheet.container,this.props.style]}>
          {status}
          {content}
        </View>;
      }
    }
    
    // 样式
    const styles = StyleSheet.create({
      container: {
        backgroundColor: "grey"
      },
      navBar: {
        justifyContent: "space-between",
        alignItem: "center",
        height: Platform.OS === "ios" ? NAV_BAR_HEIGHT_IOS : NAV_BAR_HEIGHT_ANDROID,
        flexDirection: "row"
      },
      titleViewContainer: {
        justifyContent: "center",
        alignItem: "center",
        positon: "absolute",
        left: 40,
        right: 40,
        top: 0,
        bottom: 0
      },
      title: {
        fontSize: 20,
        color: "white"
      },
      statusBar: {
        height: Platform.OS === "ios" ? STATUS_BAR_HEIGHT : 0
      }
    });
    
  • 相关阅读:
    js总结 (1)数据类型以及转换的知识整理
    伪元素 hover 的几种用法总结
    一套网页 同时适配pc端和移动端的布局思路(不要怕固定定位 和百分比)
    移动端 高亮小知识 -webkit-tap-highlight-color:transparent; tap-highlight-color:transparent;
    移动端布局 viewport 用法 简单总结
    Linux系统登陆成功和登陆失败日志的查看
    Windows系统ntlm哈希与解密、本地RDP连接密码获取
    office小技巧和一些奇怪问题的汇总解决
    阿里云镜像导出至本地操作
    sqlite数据库文件的打开与读取
  • 原文地址:https://www.cnblogs.com/YooHoeh/p/9300943.html
Copyright © 2020-2023  润新知