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

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...