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. AC日记——曼哈顿交易 洛谷 P3730

    曼哈顿交易 思路: 都是套路: 代码: #include <cmath> #include <cstdio> #include <cstring> #include ...

  2. HDU 2689.Sort it-冒泡排序

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. (1)java版本

    jdk9新增 jshell工具, 类似python的命令行界面.

  4. 网页截图工具CutyCapt

    网页截图工具CutyCapt   CuteCapt是Kali Linux提供的一款网页截图工具.该工具运行在命令行中,可以将WebKit引擎解析的网页保存为图片.它保存的文件支持矢量图和位图两大类型, ...

  5. 【记忆化搜索】bzoj1055 [HAOI2008]玩具取名

    f(l,r,c)表示sl...sr能否被合成字符c. #include<cstdio> #include<cstring> using namespace std; int m ...

  6. 使用history命令查看作业的整体执行情况

    1)通过使用history命令,我们可以深入到一个Job的任务级层面查看执行最快的任务,以及执行最慢的任务,以及其他的有用信息.命令如下: hadoop job -history /output “/ ...

  7. url参数的获取

    方法 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&am ...

  8. 【SQL】查询数据库中某个字段有重复值出现的信息

    select name,mobile from [GeneShop].[dbo].[xx_member] where mobile in ( SELECT mobile FROM [GeneShop] ...

  9. Linq To Sql的各种查询

    一.Inner Join   //request为查询条件   var result = from a in db.TableA  join b in db.TableB  on a.ID equal ...

  10. JAVA常见算法题(二十七)

    题目:给定一个存放整数的数组,请写一个算法,把偶数移动到该数组的右边,奇数放在该数组的左边,请考虑时间和空间的最优算法. package com.forezp.util; /** * 题目:给定一个存 ...