react-native開發(fā)實(shí)例之navbar

navbar組件在客戶端開發(fā)中是必備技能,使用react-native開發(fā)adr/ios通用的navbar十分簡(jiǎn)單,下面我通過使用開源組件快速的實(shí)現(xiàn)。

  • 使用代碼添加組件
npm install react-native-tab-navigator --save
  • 核心代碼如下

import React, {Component} from 'react';

import {
    Navigator,
    StyleSheet,
    Image,
    TouchableOpacity,
    View,
    Text
} from 'react-native';
import TabNavigator from 'react-native-tab-navigator';

import ApartmentListPage from './ApartmentListPage';
import ServiceListPage from './ServiceListPage';
import CustomerPage from './CustomerPage';
import ProfilePage from './ProfilePage';


const tab1 = {
    key: 'apartment',
    title: '公寓',
    img: require('../images/icon_tabbar_1.png'),
    img_on: require('../images/icon_tabbar_1_on.png'),
};

const tab2 = {
    key: 'service',
    title: '老人服務(wù)',
    img: require('../images/icon_tabbar_2.png'),
    img_on: require('../images/icon_tabbar_2_on.png'),
};


const tab3 = {
    key: 'customer',
    title: '客服咨詢',
    img: require('../images/icon_tabbar_3.png'),
    img_on: require('../images/icon_tabbar_3_on.png'),
};


const tab4 = {
    key: 'profile',
    title: '我的',
    img: require('../images/icon_tabbar_4.png'),
    img_on: require('../images/icon_tabbar_4_on.png'),
};


export default class HomePage extends Component {
    constructor(props) {
        super(props);
        this.state = {selectedTab: tab1.key}
    }

    _renderTabItem(tabJson, Comp) {

        return (
            <TabNavigator.Item
                selectedTitleStyle={styles.tabTitleStyle}
                title={tabJson.title}
                selected={this.state.selectedTab === tabJson.key}
                renderIcon={() => <Image style={styles.tabIcon} source={tabJson.img}/>}
                renderSelectedIcon={() => <Image style={styles.tabIcon} source={tabJson.img_on}/>}
                onPress={() => this.setState({selectedTab: tabJson.key})}>
                {Comp}
            </TabNavigator.Item>
        );
    }


    render() {
        return (
            <View style={{flex: 1}}>
                <TabNavigator hidesTabTouch={true} tabBarStyle={styles.tab}>
                    {this._renderTabItem(tab1, <ApartmentListPage nav={this.props.nav}/>)}
                    {this._renderTabItem(tab2, <ServiceListPage nav={this.props.nav}/>)}
                    {this._renderTabItem(tab3, <CustomerPage nav={this.props.nav}/>)}
                    {this._renderTabItem(tab4, <ProfilePage nav={this.props.nav}/>)}
                </TabNavigator>
            </View >
        );
    }
}


const styles = StyleSheet.create({
    tab: {
        height: 52,
        backgroundColor: '#F7F7FA',
        alignItems: 'center',
    },
    tabIcon: {
        width: 30,
        height: 30,
        resizeMode: 'stretch',
        marginTop: 12.5
    },
    tabTitleStyle: {
        color: '#e75404'
    },
});

  • 使用到的圖片資源
icon_tabbar_1_on.png
icon_tabbar_1.png
icon_tabbar_2_on.png
icon_tabbar_2.png
icon_tabbar_3_on.png
icon_tabbar_3.png
icon_tabbar_4_on.png
icon_tabbar_4.png
  • 最終效果如下
QQ20161215-223510.gif
  • react-native-tab-navigator組件提供了較多的style設(shè)置,我這個(gè)例子里面使用到了selectedTitleStyle這個(gè)屬性,具體清單如下。

TabNavigator props

prop default type description
sceneStyle inherited object (style) define for rendered scene
tabBarStyle inherited object (style) define style for TabBar
tabBarShadowStyle inherited object (style) define shadow style for tabBar
hidesTabTouch false boolean disable onPress opacity for Tab

TabNavigator.Item props

prop default type description
renderIcon none function returns Item icon
renderSelectedIcon none function returns selected Item icon
badgeText none string or number text for Item badge
renderBadge none function returns Item badge
title none string Item title
titleStyle inherited style styling for Item title
selectedTitleStyle none style styling for selected Item title
tabStyle inherited style styling for tab
selected none boolean return whether the item is selected
onPress none function onPress method for Item
allowFontScaling false boolean allow font scaling for title
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • demo完成之后,公司沒用,沒有精力再弄了,給大家列出學(xué)習(xí)資料 React Native 構(gòu)建 Facebook ...
    wu大維閱讀 17,304評(píng)論 8 472
  • 作者:ele828原文地址:https://github.com/ele828/react-native-guid...
    IT程序獅閱讀 45,879評(píng)論 17 380
  • React的學(xué)習(xí)資源 這個(gè)文章好久沒有更新了,資源算比較老舊的了,畢竟前端更新還是非??斓摹?半年不學(xué)習(xí),都不知道...
    izhongxia閱讀 23,615評(píng)論 11 629
  • 我覺得,最好的電影,就是能給我們講述一個(gè)好故事。 看每一個(gè)電影之前,我總是期待能看到一個(gè)好故事,看電影不單單是為了...
    韶光易逝閱讀 837評(píng)論 1 1
  • 前幾天我剛寫了一篇關(guān)于你的文章,昨晚就夢(mèng)到你,你說這是為什么呢?在那篇文章中,我寫了高中時(shí)的一些事,暗自喜...
    降降閱讀 213評(píng)論 0 0

友情鏈接更多精彩內(nèi)容