360SDN.COM

ReactNative学习十-Tab-Navigator

来源:  2017-06-18 16:49:59    评论:0点击:

1.github地址

https://github.com/exponentjs/react-native-tab-navigator

2.package.json配置

"dependencies": {
    "react-native-tab-navigator": "^0.2.15",
  }
 

3.说明

A tab bar that switches between scenes, written in JS for cross-platform support. It works on iOS and Android.This component is compatible with React Native 0.16 and newer.The look and feel is slightly different than the native navigator but it is better in some ways. Also it is pure JavaScript.The API of this component may change in the future to be more like Navigator's, which works great once there is better support for nested Navigators in React Native.

4.Usage

Import TabNavigator as a JavaScript module:

import TabNavigator from 'react-native-tab-navigator';

This is an example of how to use the component and some of the commonly used props that it supports:

<TabNavigator>
  <TabNavigator.Item
    selected={this.state.selectedTab === 'home'}
    title="Home"
    renderIcon={() => <Image source={...} />}
    renderSelectedIcon={() => <Image source={...} />}
    badgeText="1"
    onPress={() => this.setState({ selectedTab: 'home' })}>
    {homeView}
  </TabNavigator.Item>
  <TabNavigator.Item
    selected={this.state.selectedTab === 'profile'}
    title="Profile"
    renderIcon={() => <Image source={...} />}
    renderSelectedIcon={() => <Image source={...} />}
    renderBadge={() => <CustomBadgeView />}
    onPress={() => this.setState({ selectedTab: 'profile' })}>
    {profileView}
  </TabNavigator.Item>
</TabNavigator>

See TabNavigatorItem's supported props for more info.

Hiding the Tab Bar

You can hide the tab bar by using styles. For example:

let tabBarHeight = 0;
<TabNavigator
  tabBarStyle={{ height: tabBarHeight, overflow: 'hidden' }}
  sceneStyle={{ paddingBottom: tabBarHeight }}
/>

5.完整代码    'use strict';  
      
    import React, {  
        Component,  
        StyleSheet,  
        Image,  
        Text,  
        View  
    } from 'react-native';  
      
    import TabNavigator from 'react-native-tab-navigator';  
      
    const HOME = 'home';  
    const HOME_NORMAL = require('./images/tabs/home_normal.png');  
    const HOME_FOCUS = require('./images/tabs/home_focus.png');  
      
    const CATEGORY = 'category';  
    const CATEGORY_NORMAL = require('./images/tabs/category_normal.png');  
    const CATEGORY_FOCUS = require('./images/tabs/category_focus.png');  
      
    const FAXIAN = 'faxian';  
    const FAXIAN_NORMAL = require('./images/tabs/faxian_normal.png');  
    const FAXIAN_FOCUS = require('./images/tabs/faxian_focus.png');  
      
    const CART = 'cart';  
    const CART_NORMAL = require('./images/tabs/cart_normal.png');  
    const CART_FOCUS = require('./images/tabs/cart_focus.png');  
      
    const PERSONAL = 'personal';  
    const PERSONAL_NORMAL = require('./images/tabs/personal_normal.png');  
    const PERSONAL_FOCUS = require('./images/tabs/personal_focus.png');  
      
    export default class MainScreen extends Component {  
      
        constructor(props) {  
            super(props);  
            this.state = {selectedTab: HOME}  
        }  
      
        _renderTabItem(img, selectedImg, tag, childView) {  
            return (  
                <TabNavigator.Item  
                    selected={this.state.selectedTab === tag}  
                    renderIcon={() => <Image style={styles.tabIcon} source={img}/>}  
                    renderSelectedIcon={() => <Image style={styles.tabIcon} source={selectedImg}/>}  
                    onPress={() => this.setState({ selectedTab: tag })}>  
                    {childView}  
                </TabNavigator.Item>  
            );  
        }  
      
        static _createChildView(tag) {  
            return (  
                <View style={{flex:1,backgroundColor:'#00baff',alignItems:'center',justifyContent:'center'}}>  
                    <Text style={{fontSize:22}}>{tag}</Text>  
                </View>  
            )  
        }  
      
        render() {  
            return (  
                <View style={{flex: 1}}>  
                    <Header />  
                    <TabNavigator hidesTabTouch={true} tabBarStyle={styles.tab}>  
                        {this._renderTabItem(HOME_NORMAL, HOME_FOCUS, HOME, MainScreen._createChildView(HOME))}  
                        {this._renderTabItem(CATEGORY_NORMAL, CATEGORY_FOCUS, CATEGORY, MainScreen._createChildView(CATEGORY))}  
                        {this._renderTabItem(FAXIAN_NORMAL, FAXIAN_FOCUS, FAXIAN, MainScreen._createChildView(FAXIAN))}  
                        {this._renderTabItem(CART_NORMAL, CART_FOCUS, CART, MainScreen._createChildView(CART))}  
                        {this._renderTabItem(PERSONAL_NORMAL, PERSONAL_FOCUS, PERSONAL, MainScreen._createChildView(PERSONAL))}  
                    </TabNavigator>  
                </View >  
            );  
        }  
    }  
      
    const styles = StyleSheet.create({  
        tab: {  
            height: 52,  
            backgroundColor: '#303030',  
            alignItems: 'center',  
        },  
        tabIcon: {  
            width: 30,  
            height: 35,  
            resizeMode: 'stretch',  
            marginTop: 12.5  
        }  
    });

转载出处:http://blog.csdn.net/yuanguozhengjust/article/details/50553525
为您推荐

友情链接 |九搜汽车网 |手机ok生活信息网|ok生活信息网|ok微生活
 Powered by www.360SDN.COM   京ICP备11022651号-4 © 2012-2016 版权