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 3.0 键盘事件 +强制结束chromedriver进程代码

    selenium自动化测试常常用到键盘操作,一下是键盘操作的详细操作,和部分代码.截图来自于虫师的自动化相关书籍. public static void main(String[] args) thr ...

  2. 如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?

    如果不考虑意外, 第一版最简单的构思如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import getopt, sys import subpro ...

  3. PHP的文件操作类

    <?php class file { function file() { die("Class file can not instantiated!"); } //创建目录 ...

  4. Java基础:异常机制

    最近开始了找工作的面试,在面试过程中,面试官问了关于Java当中的异常处理机制,一直以来,无论写代码还是看书,自己对异常处理这一块就没有很好的重视过,对它的认知也仅仅停留在通过Try-catch去进行 ...

  5. linux进程通信之管道

    1.介绍: 1)同一主机: unix进程通信方式:无名管道,有名管道,信号 system v方式:信号量,消息队列,共享内存 2)网络通信:Socket,RPC 2.管道: 无名管道(PIPE):使用 ...

  6. Maven学习笔记1

    Maven是什么? 百度百科:Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 这些描述总是让人更加难理解Maven,扔掉它,咱们先看看Mave ...

  7. DP重开

    颓了差不多一周后,决定重开DP 这一周,怎么说,学了学trie树,学了学二叉堆,又学了学树状数组,差不多就这样,然后和cdc一番交流后发现,学这么多有用吗?noip的范围不就是提高篇向外扩展一下,现在 ...

  8. Codeforces #430 Div2 C

    #430 Div2 C 题意 给出一棵带点权的树,每一个节点的答案为从当前节点到根节点路径上所有节点权值的最大公因子(在求最大共因子的时候可以选择把这条路径上的任意一点的权值置为0).对于每一个节点单 ...

  9. 【前缀和】【分类讨论】hdu5163 Taking Bus

    #include<cstdio> using namespace std; int T,n,m,x,y; long long sum[100001],ans,d[100001]; int ...

  10. 【tarjan求割顶】BZOJ2730-[HNOI2012]矿场搭建

    [题目大意] 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖煤点坍 ...