React-Native实现TabBar切换界面

实现Tabbar切换的效果是引用的第三方(react-native-tab-navigator)插件
1.导入此插件-npm install react-native-tab-navigator
2.引用-import TabNavigator from 'react-native-tab-navigator';
使用的是ES6语法

效果图:

React-Native实现TabBar切换界面_第1张图片
Paste_Image.png

代码:

/**
 * 1. npm install react-native-tab-navigator
 * 2. import TabNavigator from 'react-native-tab-navigator';
 */

import React, { Component } from 'react';
import {
  StyleSheet,
  Platform,
  Image,
  Navigator,
  View,
  Text,
  TouchableOpacity
} from 'react-native';

import TabNavigator from 'react-native-tab-navigator';
import Home from '../home/YhHome';
import Mine from '../mine/YhMine';
import More from '../more/YhMore';
import Shop from '../shop/YhShop';
const styles = StyleSheet.create({
  titleStyle: {
    fontSize: 16,
    color: '#515151'
  },
  tab: {
    height: 52,
    alignItems: 'center',
    backgroundColor: 'rgb(241, 241, 241)'
  },
  iconStyle: {
    width: Platform.OS === 'ios' ? 26 : 25,
    height: Platform.OS === 'ios' ? 26 : 25
  },
  selectedTitleStyle: {
    color: 'orange'
  },
  navBar: {
    backgroundColor: '#03a9f4',
    height: (Platform.OS === 'ios') ? 64 : 44
  },
  navBarLeftButton: {
    flex: 1,
    width: 50,
    height: 50,
    alignItems: 'center',
    justifyContent: 'center',
    paddingLeft: 5
  },
  navBarRightButton: {
    marginRight: 5
  },
  navBarLeftTitle: {
    fontSize: 18,
    color: '#FFFFFF'
  },
  navBarTitleText: {
    fontWeight: '500',
    fontSize: 20,
    color: '#FFFFFF',
    marginTop: 10
  },
  icon: {
    width: Platform.OS === 'ios' ? 22 : 24,
    height: Platform.OS === 'ios' ? 20 : 24
  }
});

export default class YhTabBar extends Component {

  constructor(props) {
    super(props);
    this.state = {
      selectedTab: 'home'
    };
  }

  renderTabBarItem(title, iconName, iconSelectName, selectedTab, componentName, component) {
    return(
       }
        renderSelectedIcon={() => }
        selected={this.state.selectedTab === selectedTab}
        onPress={()=>{this.setState({selectedTab:selectedTab})}}
        titleStyle={styles.titleStyle}
        selectedTitleStyle={styles.selectedTitleStyle}
      >
         {
            return Navigator.SceneConfigs.PushFromRight;
          }}
          renderScene={(route, navigator) => {
            let MyComponent = route.component;
            return ;
          }}
          navigationBar={this.navBar()}
        />
      
    )
  }

  render() {
    return (
      
        {/*--首页--*/}
        {this.renderTabBarItem('首页', 'icon_tabbar_homepage', 'icon_tabbar_homepage_selected', 'home', '主页', Home)}
        {/*--商家--*/}
        {this.renderTabBarItem('商家', 'icon_tabbar_merchant_normal', 'icon_tabbar_merchant_selected', '商家', 'shop', Shop)}
        {/*--我的--*/}
        {this.renderTabBarItem('我的', 'icon_tabbar_mine', 'icon_tabbar_mine_selected', 'mine', '我的', Mine)}
        {/*--更多--*/}
        {this.renderTabBarItem('更多', 'icon_tabbar_misc', 'icon_tabbar_misc_selected', 'more', '更多', More)}
      
    );
  }


  navBar() {
      return (
         {
            },
            Title: (route) => this.setTitle(route.title)
          }}
        />
      );
  }

  LeftButton = (route, navigator, index) => {
    if (index > 0) {
      return (
         {
              navigator.pop();
          }}
        >
         返回
        
      );
    } else {
      return null
    }
  }

  setTitle(title){
    return (
      
        
          {title}
        
      
    );
  }
}



你可能感兴趣的:(React-Native实现TabBar切换界面)