码迷,mamicode.com
首页 > 其他好文 > 详细

react-native + react-native-tab-navigator 实现 TabBar

时间:2018-08-26 11:45:33      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:state   navigator   create   self   sele   nbsp   xtend   car   creat   

1.安装 react-native-tab-navigator

yarn add react-native-tab-navigator

2.页面调用

/**
 * 主页面
 */
import React, {Component} from ‘react‘;
import {
  View,
  Image,
  StyleSheet
} from ‘react-native‘;
// 底部导航栏
import TabNavigator from ‘react-native-tab-navigator‘;
// 首页
import HomePage from ‘./HomePage‘;
// 购物车
import ShopCarPage from ‘./ShopCarPage‘;
// 我的
import MinePage from ‘./MinePage‘;
// 图片资源
import { images } from ‘../../res‘;

const dataSource = [
  {
    icon:images.tabbar_home_normal,
    selectedIcon:images.tabbar_home_selected,
    tabPage:‘Home‘,
    tabName:‘首页‘,
    component:HomePage
  },
  {
    icon:images.tabbar_shopcar_normal,
    selectedIcon:images.tabbar_shopcar_selected,
    tabPage:‘ShopCar‘,
    tabName:‘购物车‘,
    component:ShopCarPage
  },
  {
    icon:images.tabbar_mine_normal,
    selectedIcon:images.tabbar_mine_selected,
    tabPage:‘Mine‘,
    tabName:‘我的‘,
    component:MinePage
  }
];

var navigation = null;

export default class MainPage extends Component {

  constructor(props) {
    super(props);
    navigation = this.props.navigation;
    this.state = {
      selectedTab:‘Home‘
    };
  }

  render() {

    let tabViews = dataSource.map((item,i) => {
      return (
          <TabNavigator.Item
            title={item.tabName}
            selected={this.state.selectedTab===item.tabPage}
            titleStyle={{color:‘#999999‘}}
            selectedTitleStyle={{color:‘#ED5100‘}}
            renderIcon={()=><Image style={styles.tabIcon} source={item.icon}/>}
            renderSelectedIcon = {() => <Image style={styles.tabIcon} source={item.selectedIcon}/>}
            tabStyle={{alignSelf:‘center‘}}
            onPress = {() => {this.setState({selectedTab:item.tabPage})}}
            key={i}
            >
            <item.component navigation={navigation}/>
        </TabNavigator.Item>
      );
    });

    return (
      <View style={styles.container}>
        <TabNavigator
          hidesTabTouch={true}
          >
            {tabViews}
        </TabNavigator>
      </View>
    );
  }  
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: ‘#F5FCFF‘,
  },
  tabIcon:{
    width:23,
    height:23,
  }
});

3.效果图

技术分享图片

react-native + react-native-tab-navigator 实现 TabBar

标签:state   navigator   create   self   sele   nbsp   xtend   car   creat   

原文地址:https://www.cnblogs.com/crazycode2/p/9536507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!