react-native-scrollable-tab-view demo

TarBar 可使用跨平台的 scrollable-tab-view

tabbar.png

默认的 DefaultTabBar 跟我们的样式不大一样,所以需要自定义 TabBar
参考网上代码修改了一下

自定义 TabBar

/* @flow */
import React, { Component } from 'react';
import {
  View,
  Text,
  StyleSheet,
  TouchableOpacity,
  Image
} from 'react-native';

export default class CustomerBar extends Component {
  static propTypes = {
    goToPage: React.PropTypes.func, // 跳转到对应tab的方法
    activeTab: React.PropTypes.number, // 当前被选中的tab下标
    tabs: React.PropTypes.array, // 所有tabs集合
    tabNames: React.PropTypes.array, // 保存Tab名称
    tabIcons: React.PropTypes.array, // 默认图标
    tabSelectedIcons: React.PropTypes.array, // 选中图标,
  }

  render() {
    return (
      <View>
        <View style={{height:1, backgroundColor:'grey'}}></View> // tabbar 顶部加一条横线
        <View style={styles.tabs}>
            {this.props.tabs.map((tab, i) => this.renderTabOption(tab, i))}
        </View>
      </View>
    );
  }

  renderTabOption(tab, i) {
    const color = this.props.activeTab == i? "green" : "black"; // 判断i是否是当前选中的tab,设置不同的颜色
    const icon = this.props.activeTab == i ? this.props.tabSelectedIcons[i] : this.props.tabIcons[i];

    return (
        <TouchableOpacity onPress={()=>this.props.goToPage(i)} style={styles.tab} key={i}>
            <View style={styles.tabItem}>
                <Image
                    source={icon}  // 图标
                    style={{height:25}}
                    />
                <Text style={{color: color,marginTop:3, fontSize:12}}>
                    {this.props.tabNames[i]}
                </Text>
            </View>
        </TouchableOpacity>
     );
  }
}

const styles = StyleSheet.create({
    tabs: {
        flexDirection: 'row',
    },

    tab: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },

    tabItem: {
        alignItems: 'center',
    paddingTop:3,
    paddingBottom:3
    },
});
// @flow
import React, {
    Component
} from 'react';
import {
    AppRegistry,
    Text,
    StyleSheet,
    Platform
} from 'react-native';

var ScrollableTabView = require('react-native-scrollable-tab-view');
import CustomerBar from './components/CustomerBar'

var App = React.createClass({
  tabNames: ['互助','凭证','公示','我的'],
  tabIcons:[require('./images/home.png'),require('./images/cert.png'),require('./images/notice.png'),require('./images/profile.png')],
  tabSelectedIcons:[require('./images/home_sel.png'),require('./images/cert_sel.png'),require('./images/notice_sel.png'),require('./images/profile_sel.png')],

  render() {
    return (
      <ScrollableTabView
        tabBarPosition='bottom' // tab 底部显示
        locked = {true} // 禁止左右滑动
        scrollWithoutAnimation = {true} // 切换页面时不显示动画
        renderTabBar={
          () => <CustomerBar
                  tabNames={this.tabNames}
                  tabIcons = {this.tabIcons}
                  tabSelectedIcons={this.tabSelectedIcons}
         />}>
        <Text style={styles.content}>#Page1</Text>
        <Text style={styles.content}>#Page2</Text>
        <Text style={styles.content}>#Page3</Text>
        <Text style={styles.content}>#Page4</Text>
      </ScrollableTabView>
    );
  }
});

const styles = StyleSheet.create({
  content: {
    marginTop: (Platform.OS === 'ios')? 20 : 0 // IOS 顶部加 20
  }
});

AppRegistry.registerComponent('HelloReact',() => App);

Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,357评论 25 708
  • 这是一件温暖的事情。 张鑫去帮忙,回家的时候已是午夜十二点半了。 十二点半的天好热,张鑫驾驶着农用车,尽管农用车发...
    往事不再随风阅读 495评论 0 0
  • 今天终于可以出走了,这不是一场说走就走的旅行,而是提前请了假,提前确定出走日期的旅行,虽然我不知道具体路线,但正因...
    婉慧阅读 652评论 2 0
  • 希望你也能记住我,记住我曾这样存在过 挪威的森林。最开始这个名字与我的生命有印象较深的交集,是伍佰的那首歌。 那时...
    Jasmim阅读 1,176评论 0 0