1.创建组件

src/components/CustomTabBar/index.js

/**
* 自定义选项卡
*/
import React, {Component} from 'react';
import {
Platform,
StyleSheet,
StatusBar,
View,
TouchableOpacity,
Image,
Text,
} from 'react-native';
//第三方插件
import PropTypes from 'prop-types';
//自定义组件
import Common from '../../common'; //公共类
// 图片资源
import { images } from '../../res'; const tabIcons = [
images.tabbar_home_selected,
images.tabbar_home_normal,
images.tabbar_shopcar_selected,
images.tabbar_shopcar_normal,
images.tabbar_mine_selected,
images.tabbar_mine_normal
]; export default class CustomTabBar extends Component {
constructor(props) {
super(props);
} static setAnimationValue({value}) {
console.log(value);
} componentDidMount() {
// Animated.Value监听范围 [0, tab数量-1]
this.props.scrollValue.addListener(CustomTabBar.setAnimationValue);
} renderTabOption(tab, i) {
let color = this.props.activeTab === i ? "#ED5100" : "#999999"; // 判断i是否是当前选中的tab,设置不同的颜色
let tabName = this.props.tabNames[i];
return (
<TouchableOpacity onPress={()=>this.props.goToPage(i)} style={[styles.tab]} key={'tab' + i}>
<View style={[styles.tabBox]}>
<Image
source={tabIcons[this.props.activeTab === i ? i*2 : i*2+1]}
style={[styles.tabBoxIcon]}
/>
<Text style={[styles.tabBoxName, {color: color}]}>
{tabName}
</Text>
</View>
</TouchableOpacity>
);
} renderTabs() {
if (true !== this.props.placeMiddle || 0 !== this.props.tabs.length%2) {
return this.props.tabs.map((tab, i) => this.renderTabOption(tab, i));
} else {
let tabs = [];
for (let i = 0; i < this.props.tabs.length; i++) {
let tab = this.props.tabs[i];
if (i === parseInt(this.props.tabs.length/2)) {
let middle = (
<View key={'tabMiddle'} style={[styles.tab]}>
<View style={[styles.tabMiddleBox]}/>
</View>
);
tabs.push(middle);
}
tabs.push(this.renderTabOption(tab, i));
}
return tabs;
}
} render() {
let tabBarHeight = Platform.select({
ios: Common.isIphoneX ? 68 : 49,
android: 49,
});
return (
<View key={'custom'} style={[styles.tabs, {height: tabBarHeight}]}>
<StatusBar
backgroundColor="#ffffff"
barStyle="dark-content"
/>
{this.renderTabs()}
</View>
);
}
} CustomTabBar.propTypes = {
goToPage: PropTypes.func, // 跳转到对应tab的方法
activeTab: PropTypes.number, // 当前被选中的tab下标
tabs: PropTypes.array, // 所有tabs集合
tabNames: PropTypes.array, // 保存Tab名称
tabIconNames: PropTypes.array, // 保存Tab图标
}; const styles = StyleSheet.create({
tabs: {
flexDirection: 'row',
backgroundColor:'#ffffff',
borderTopWidth: 0.5,
borderTopColor: '#cdcdcd',
},
tab: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
},
tabBox: {
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
width: 48,
height: 48,
},
tabMiddleBox: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
width: 48,
height: 48,
},
tabBoxIcon: {
width: 22,
height: 22,
},
tabBoxName: {
fontSize: 10,
marginTop: 3,
},
});

2.页面调用

src/pages/MainPage/index.js

/**
* 主页面
*/
import React, {Component} from 'react';
import {
BackHandler, // 物理返回键
View,
ToastAndroid,
Image,
StyleSheet
} from 'react-native';
import ScrollableTabView from 'react-native-scrollable-tab-view';
// 自定义选项卡
import { CustomTabBar } from '../../components';
// 首页
import HomePage from './HomePage';
// 购物车
import ShopCarPage from './ShopCarPage';
// 我的
import MinePage from './MinePage'; export default class MainPage extends Component { render() {
let tabNames = ['首页', '购物车', '我的']; return (
<ScrollableTabView
initialPage={0} //初始tab索引
renderTabBar={() =>
<CustomTabBar
tabNames={tabNames} //tab名称
placeMiddle={false} //中间是否占位,即中间是否需要用特殊按钮样式等
/>
}
tabBarPosition='bottom'
>
<HomePage key='homePage' tabLabel='home' /> <ShopCarPage key='ShopCarPage' tabLabel='shopCar' /> <MinePage key='minePage' tabLabel='mine' />
</ScrollableTabView>
);
}
} const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
tabIcon:{
width:23,
height:23,
}
});

3.效果图

react-native-scrollable-tab-view 实现 TabBar的更多相关文章

  1. react native底部tab栏切换

    1.安装tab栏插件 npm i react-native-tab-navigator --save 2.引入对应的组件和tab插件 import { Platform, StyleSheet, Te ...

  2. H5、React Native、Native应用对比分析

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博!iOS开发者交流QQ群: 446310206 "存在即合理".凡是存在的,都是合乎规律的.任何新 ...

  3. React Native八大Demo

    参考资料:http://www.cnblogs.com/shaoting/p/7148240.html 下一个项目公司也打算使用react native.大致看了下原型设计,写几个小demo先试试水. ...

  4. 从web移动端布局到react native布局

    在web移动端通常会有这样的需求,实现上中下三栏布局(上下导航栏位置固定,中间部分内容超出可滚动),如下图所示: 实现方法如下: HTML结构: <div class='container'&g ...

  5. Hybrid App 和 React Native 开发那点事

    简介:Hybrid App(混合模式移动应用)开发是指介于Web-app.Native-App这两者之间的一种开发模式,兼具「Native App 良好用户交互体验的优势」和「Web App 跨平台开 ...

  6. 【React Native】在原生和React Native间通信(RN调用原生)

    一.从React Native中调用原生方法(原生模块) 原生模块是JS中也可以使用的Objective-C类.一般来说这样的每一个模块的实例都是在每一次通过JS bridge通信时创建的.他们可以导 ...

  7. React Native 系列(九) -- Tab标签组件

    前言 本系列是基于React Native版本号0.44.3写的.很多的App都使用了Tab标签组件,例如QQ,微信等等,就是切换不同的选项,显示不同的内容.那么这篇文章将介绍RN中的Tab标签组件. ...

  8. React Native 之 View使用

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

  9. React Native 开发之 (07) 常用组件-View

    掌握了React Native的组件就可以使用IOS的原生组件和API. 一 View组件 就像开发web应用程序中,需要使用很多的HTML标签.例如 div,form.但是在基于DIV+CSS布局的 ...

  10. [React Native] Complete the Notes view

    In this final React Native lesson of the series we will finalize the Notes view component and squash ...

随机推荐

  1. selenium java 读取xml (数据驱动)

    selenium 数据驱动 (xml解析) getElementByTagName()可以通过标签名获取某个标签.它所获取的对象是以数组形式存放.如“caption”和“item”标签在info.xm ...

  2. Centos 下,配置 Apache + Python + Django + postgresSQL 开发环境

    用 Python 搭建一个 Web 服务器 文章结构 一.安装  Apache.Python.django 二.安装 mod_wsgi,Apache 为 Python 提供的 wsgi 模块  三.将 ...

  3. 跳转的两种实现方法setInterval和setTimeout

    setInterval方法: <html> <head> <meta http-equiv="Content-Type" content=" ...

  4. 51nod 1265 四点共面【计算几何+线性代数】

    1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...

  5. Spfa+DP【p2149】[SDOI2009]Elaxia的路线

    Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. Elaxia和w**每天都要奔波于宿舍和实验室之间 ...

  6. 学习LSM(Linux security module)之二:编写并运行一个简单的demo

    各种折腾,经过了一个蛋疼的周末,终于在Ubuntu14.04上运行了一个基于LSM的简单demo程序. 一:程序编写 先简单的看一下这个demo: //demo_lsm.c#include <l ...

  7. mysql实现full outer join

    由于MySQL设计时不支持full outer join, 所以如果有全连接需求时,需要一点小技巧来实现. 假设有两个表t1,t2 full outer join 的效果和下面的效果一样: SELEC ...

  8. POJ 3246 Game(凸包)

    [题目链接] http://poj.org/problem?id=3246 [题目大意] 给出一些点,请删去一个点,使得包围这些点用的线长最短 [题解] 去掉的点肯定是凸包上的点,所以枚举凸包上的点去 ...

  9. 【贪心】【堆】bzoj1029 [JSOI2007]建筑抢修

    按完成时限排序,一个个修复.若当前建筑花费时间+之前花费的总时间不超过时限,则ans++:否则,从之前已修复的建筑中挑一个耗时最多的,与当前建筑比较,若当前建筑更优,则更新ans. #include& ...

  10. boost 1.57 vs2013 编译

    启动vs2013中的命令行注意区分32/64, 进入boost目录,  再次运行 bootstrap.bat 编译: bjam.exe stage --toolset=msvc-12.0  --sta ...