ReactNative学习十-Tab-Navigator

前端之家收集整理的这篇文章主要介绍了ReactNative学习十-Tab-Navigator前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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>} badgeText"1" onPress=> this.setState({ selectedTab: 'home' })}> {homeView} </TabNavigator.Item'profile'} title"Profile" renderIcon>} renderBadge<CustomBadgeView >} onPress'profile' })}> {profileView} > /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://www.jb51.cc/article/p-uulzskdg-bgm.html 原文链接:https://www.f2er.com/react/306942.html

猜你在找的React相关文章