前言

  • 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习

  • 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所偏差,在学习中如果有错会及时修改内容,也欢迎万能的朋友们批评指出,谢谢

  • 文章第一版出自简书,如果出现图片或页面显示问题,烦请转至 简书 查看 也希望喜欢的朋友可以点赞,谢谢

TabBarIOS 组件简介


  • 目前的APP内,大部分都是选项与选项之间切换,比如:微信、微博、QQ空间…,在iOS中,我们可以通过TabItem类进行实现,那么,在React Native中,我们可以通过TabBarIOS和TabBarIOS.Item组件来实现选项卡切换效果,大家可以看到后面带有IOS,所以这个组件不支持Android,当然后面我们会通过自定义该组件来满足实际开发需求
  • 当然,本章涉及到了 TabBarIOS组件 ,那么必不可少的,肯定需要与 TabBarIOS.Item 来搭配使用,废话不多说,先来看它们各自都拥有哪些属性

TabBarIOS 常见属性


  • 继承了View的所有属性

  • barTintColor:标签栏的背景颜色

  • tintColor:当前被选中的标签图标颜色

  • translucent:bool值,决定标签栏是否需要半透明化

TabBarIOS.Item 常见属性


  • 继承了View的所有属性

  • badge:图标右上角显示的红色角标

  • icon:给当前标签指定一个自定义图标(如果定义了 systemIcon属性 这个属性会被忽略)

  • onPress:点此标签被选中时调用,你应该修改过组件的状态使 selected={true}

  • selected:这个属性决定了子视图是否可见,如果你看到一个空白的页面,很可能是没有选中任何一个标签

  • selectedIcon:当标签被选中的时候显示的自定义图标(如果定义了systemIcon属性,这个属性会被忽略,如果定义了icon而没定义这个属性,在选中的时候图标会被染上蓝色)

  • systemIcom:一些预定义的系统图标(如果使用了此属性,标题和自定义图标都会被覆盖为系统定义的值)

    • 默认值:'bookmarks', 'contacts', 'downloads', 'favorites', 'featured', 'history', 'more', 'most-recent', 'most-viewed', 'recents', 'search', 'top-rated'
  • title:在图标下面显示的标题文字(如果定义了 systemIcon属性,这个属性会被忽略)

TabBarIOS 初体验


  • 先简单来看下怎么使用TabBarIOS

    • 首先我们需要引入TabBarIOS
    	import {
    TabBarIOS
    } from 'react-native';
    • 使用 TabBarIOS 很简单,但是需要配合 TabBarIOS.Item 使用,(需要注意的是我们必须给TabBarIOS设置尺寸,不然可能会造成实例化却无法看到的问题)
    	render() {
    return (
    <View style={styles.container}>
    <TabBarIOS
    style={{height:49, width: width}}
    >
    </TabBarIOS>
    </View>
    );
    }

    效果:

    • 接着我们来给它添加 Item(TabBarIOS最多只能包含5个Item,超出的部分会用 more图标 代替)
    	render() {
    return (
    <View style={styles.container}>
    <TabBarIOS
    style={{height:49, width: width}}
    >
    <TabBarIOS.Item
    systemIcon="bookmarks" // 系统图标(bookmarks)
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="contacts" // 系统图标(contacts)
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="downloads" // 系统图标(downloads)
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="favorites" // 系统图标(favorites)
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="history" // 系统图标(history)
    >
    </TabBarIOS.Item>
    </TabBarIOS>
    </View>
    );
    }

    效果:

  • 是不是很简单,接下来我们试着修改一下 TabBarIOS 的属性,看看效果怎样样

    • 当前被选中标签颜色
    	<TabBarIOS
    style={{height:49, width: width}}
    tintColor="green" // 被选中标签颜色
    >
    </TabBarIOS>

    效果:

    • 背景色
    	<TabBarIOS
    style={{height:49, width: width}}
    tintColor="green"
    barTintColor="black" // TabBarIOS背景色
    >
    </TabBarIOS>

    效果:

    • 是否有半透明效果
    	<TabBarIOS
    style={{height:49, width: width}}
    tintColor="green"
    barTintColor="black"
    translucent={false} // TabBarIOS不需要半透明效果
    >
    </TabBarIOS>

    效果:

  • 这边再来试试 TabBarIOS.Item 的属性

    • 系统自带图标

      • bookmarks
      	<TabBarIOS.Item
      systemIcon="bookmarks" // 系统图标(bookmarks)
      >
      </TabBarIOS.Item>

      效果:

      • contacts
      	<TabBarIOS.Item
      systemIcon="contacts" // 系统图标(contacts)
      >
      </TabBarIOS.Item>

      效果:

      • downloads
      	<TabBarIOS.Item
      systemIcon="downloads" // 系统图标(downloads)
      >
      </TabBarIOS.Item>

      效果:

      • favorites
      	<TabBarIOS.Item
      systemIcon="favorites" // 系统图标(favorites)
      >
      </TabBarIOS.Item>

      效果:

      • featured
      	<TabBarIOS.Item
      systemIcon="featured" // 系统图标(featured)
      >
      </TabBarIOS.Item>

      效果:

      • history
      	<TabBarIOS.Item
      systemIcon="history" // 系统图标(history)
      >
      </TabBarIOS.Item>

      效果:

      • more
      	<TabBarIOS.Item
      systemIcon="more" // 系统图标(more)
      >
      </TabBarIOS.Item>

      效果:

      • most-recent
      	<TabBarIOS.Item
      systemIcon="most-recent" // 系统图标(most-recent)
      >
      </TabBarIOS.Item>

      效果:

      • most-viewed
      	<TabBarIOS.Item
      systemIcon="most-viewed" // 系统图标(most-viewed)
      >
      </TabBarIOS.Item>

      效果:

      • recents
      	<TabBarIOS.Item
      systemIcon="recents" // 系统图标(recents)
      >
      </TabBarIOS.Item>

      效果:

      • search
      	<TabBarIOS.Item
      systemIcon="search" // 系统图标(search)
      >
      </TabBarIOS.Item>

      效果:

      • top-rated
      	<TabBarIOS.Item
      systemIcon="top-rated" // 系统图标(top-rated)
      >
      </TabBarIOS.Item>

      效果:

    • 角标(角标的位置会受到TabBarIOS右边空间音效,当位置不够时,会自动往左移动,以保证显示完整性)

    	<TabBarIOS
    style={{height:49, width: width}}
    tintColor="green"
    barTintColor="black"
    translucent={false}
    >
    <TabBarIOS.Item
    systemIcon="bookmarks" // 系统图标(bookmarks)
    badge="99999999"
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="contacts" // 系统图标(contacts)
    badge="15"
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="downloads" // 系统图标(downloads)
    badge="@$!@"
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="favorites" // 系统图标(favorites)
    badge="aBBc"
    >
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="history" // 系统图标(history)
    badge="99+"
    >
    </TabBarIOS.Item>
    </TabBarIOS>

    效果:

    • 自定义图标(目前只支持本地图片)
    	<TabBarIOS.Item
    renderAsOriginal={true} // 如果为false,只会显示纯色图片
    icon={require('image!home')}
    >
    </TabBarIOS.Item>

    效果:

    • 自定义高亮图标(目前只支持本地图片,如果没有设置,则会显示选中颜色图标)
    	selectedIcon={require('image!baker')}
    
    

    效果:

    • 文字(如果设置了系统图标,那么这个属性会被忽略)
    	title="首页"
    
    

    效果:

TabBarIOS.Item点击


  • 到这里肯定有人会说,为什么我的 TabBarIOS.Item 不能接收点击事件,无法切换界面,这边就来讲解怎么去实现页面的切换,它涉及到 TabBarIOS.Item 的两个属性 —— selectedonPress

    • 首先,要实现页面之间的切换,那么首先它们自己要有对应的页面,这边先来给各个 Item 设置属于自己的页面
    	render() {
    return (
    <View style={styles.container}>
    <TabBarIOS
    style={{height:49, width: width}}
    tintColor="green"
    barTintColor="black"
    translucent={false}
    >
    <TabBarIOS.Item
    systemIcon="bookmarks" // 系统图标(bookmarks)
    >
    <View style={[styles.childViewStyle, {backgroundColor:'yellow'}]}> </View>
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="contacts" // 系统图标(contacts)
    >
    <View style={[styles.childViewStyle, {backgroundColor:'blue'}]}> </View>
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="downloads" // 系统图标(downloads)
    >
    <View style={[styles.childViewStyle, {backgroundColor:'red'}]}> </View>
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="favorites" // 系统图标(favorites)
    >
    <View style={[styles.childViewStyle, {backgroundColor:'green'}]}> </View>
    </TabBarIOS.Item>
    <TabBarIOS.Item
    systemIcon="history" // 系统图标(history)
    >
    <View style={[styles.childViewStyle, {backgroundColor:'gray'}]}> </View>
    </TabBarIOS.Item>
    </TabBarIOS>
    </View>
    );
    }
    • 页面之间的切换其实就是根据 selected 是否为 ture,以此决定是否重新渲染界面,涉及到重新渲染,所以肯定需要使用到 getInitialState(状态机) ,具体操作如下

      • 首先我们定义一个初始化变量来确定要显示的页面
      	getInitialState(){
      return{
      selectedTabItem:0 // 预设变量,记录当前点击的item
      }
      },
      • 当我们点击相应标签的时候,系统就会调用 onPress 属性来进行反馈

        • 首先点击onPress的时候我们需要更新 状态机 中预设变量的值
        	onPress={() => {this.setState({selectedTabItem:0})}}
        
        
        • 接着我们要根据 预设变量 来判断跳转到哪个页面(当预设变量的值改变后,状态机会再次调用 render 函数进行渲染,也就会调用 TabBarIOS.Item 内的 selected 属性)
        	selected={this.state.selectedTabItem == 0}
        
        
        • 视图部分完整代码
        	var TabBarIOSDemo = React.createClass({
        
        		getInitialState(){
        return{
        selectedTabItem:0
        }
        }, render() {
        return (
        <View style={styles.container}>
        <TabBarIOS
        style={{height:49, width: width}}
        tintColor="green"
        barTintColor="black"
        translucent={false}
        >
        <TabBarIOS.Item
        systemIcon="bookmarks" // 系统图标(bookmarks)
        onPress={() => {this.setState({selectedTabItem:0})}}
        selected={this.state.selectedTabItem == 0}
        >
        <View style={[styles.childViewStyle, {backgroundColor:'yellow'}]}> </View>
        </TabBarIOS.Item>
        <TabBarIOS.Item
        systemIcon="contacts" // 系统图标(contacts)
        onPress={() => {this.setState({selectedTabItem:1})}}
        selected={this.state.selectedTabItem == 1}
        >
        <View style={[styles.childViewStyle, {backgroundColor:'blue'}]}> </View>
        </TabBarIOS.Item>
        <TabBarIOS.Item
        systemIcon="downloads" // 系统图标(downloads)
        onPress={() => {this.setState({selectedTabItem:2})}}
        selected={this.state.selectedTabItem == 2}
        >
        <View style={[styles.childViewStyle, {backgroundColor:'red'}]}> </View>
        </TabBarIOS.Item>
        <TabBarIOS.Item
        systemIcon="favorites" // 系统图标(favorites)
        onPress={() => {this.setState({selectedTabItem:3})}}
        selected={this.state.selectedTabItem == 3}
        >
        <View style={[styles.childViewStyle, {backgroundColor:'green'}]}> </View>
        </TabBarIOS.Item>
        <TabBarIOS.Item
        systemIcon="history" // 系统图标(history)
        onPress={() => {this.setState({selectedTabItem:4})}}
        selected={this.state.selectedTabItem == 4}
        >
        <View style={[styles.childViewStyle, {backgroundColor:'gray'}]}> </View>
        </TabBarIOS.Item>
        </TabBarIOS>
        </View>
        );
        }
        });

        效果:

  • 到这里,TabBarIOS页面切换就完成了,当然实际开发中我们会抽取代码,使代码看起来不会这么杂乱,这在后面会通过综合项目进行讲解

补充


  • 上面出现这样的代码,可能很多初学者不知道什么意思,这边就补充说明一下,在JS中是允许多个样式通过数组的形式传递的,它会根据数组内容自动去解析需要的值,并根据优先级去选择优先选择使用哪个属性

React-Native 之 TabBarIOS的更多相关文章

  1. React Native 之TabBarIOS

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  2. React Native 之 项目实战(一)

    前言 本文有配套视频,可以酌情观看. 文中内容因各人理解不同,可能会有所偏差,欢迎朋友们联系我. 文中所有内容仅供学习交流之用,不可用于商业用途,如因此引起的相关法律法规责任,与我无关. 如文中内容对 ...

  3. React Native组件之ScrollView 和 StatusBar和TabBarIos

    React Native中的组件ScrollView类似于iOS中的UIScrollView,其基本的使用方法和熟悉如下: /** * Sample React Native App * https: ...

  4. React Native常用组件之TabBarIOS、TabBarIOS.Item组件、Navigator组件、NavigatorIOS组件、React Navigation第三方

    以下内容为老版本React Native,faceBook已经有了新的导航组件,请移步其他博客参考>>[我是传送门] 参考资料:React Navigation  react-native ...

  5. React Native组件介绍

    1.React Native目前已有的组件 ActivityIndicatorIOS:标准的旋转进度轮; DatePickerIOS:日期选择器: Image:图片控件: ListView:列表控件: ...

  6. React Native之 Navigator与NavigatorIOS使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  7. React Native也正式发布了

    var React = require('react-native'); var { TabBarIOS, NavigatorIOS } = React; var App = React.create ...

  8. React Native开发技术周报2

    (1).资讯 1.React Native 0.22_rc版本发布 添加了热自动重载功能 (2).技术文章 1.用 React Native 设计的第一个 iOS 应用 我们想为用户设计一款移动端的应 ...

  9. React Native 简介:用 JavaScript 搭建 iOS 应用(2)

    [编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...

  10. React Native 简介:用 JavaScript 搭建 iOS 应用 (1)

    [编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...

随机推荐

  1. extjs几个奇怪的错误

    在用Extjs进行网页开发的时候,遇见了一下两个错误,这两个错误的位置用firebug调试显示在extjs-all.js Ext.resetElement is undefined g.el is n ...

  2. ELK 性能(1) — Logstash 性能及其替代方案

    ELK 性能(1) - Logstash 性能及其替代方案 介绍 当谈及集中日志到 Elasticsearch 时,首先想到的日志传输(log shipper)就是 Logstash.开发者听说过它, ...

  3. 组件 -- Button

    .btn --------------------------------- button的背景色: .btn-primary .btn-success .btn-secondary .btn-dan ...

  4. Django如何安装指定版本

      Django默认安装最新版本:pip install django Django后面接版本号就可以了:pip install django==1.11.7 如果使用pip install安装库比较 ...

  5. Docker inside Docker 基于 Alpine Linux

    Study From https://hub.docker.com/_/docker/ 感慨一句 这些人真牛B .. 简单测试 拉取镜像 docker pull docker:dind 运行镜像 do ...

  6. css命名管理混乱?不妨试试BEM

    css的缺陷 我们知道,css使用中一个比较令人烦恼的问题,就是css没有作用域可言,我们写了一个组件或者模块之后,往往希望它们可以四处复用,但是由于css没有作用域,我们给样式命名的时候都会非常小心 ...

  7. poj1064 Cable master

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  8. 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并

    题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...

  9. Spring定时服务QuartZ

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等. 我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作, ...

  10. c# winform 为按钮动态背景图片

    参考自:http://www.cnblogs.com/sufei/archive/2012/11/15/2771299.html 第一种,使用Properties.Resources类,这种方法需要你 ...