導(dǎo)航一直是App開發(fā)中比較重要的一個(gè)組件,ReactNative提供了兩種導(dǎo)航組件供我們使用,分別是:NavigatorIOS和Navigator,但是前者只能用于iOS平臺(tái),后者在ReactNative0.44版本以后已經(jīng)被移除了。
好在有人提供了更好的導(dǎo)航組件,就是我們今天要講的react-navigation,并且ReactNative官方更推薦我們使用此組件。
本篇文章只講解基礎(chǔ)用法,如果你想了解更多,請(qǐng)戳這里->戳我。
簡介
react-navigation主要包括導(dǎo)航,底部tab,頂部tab,側(cè)滑等,分別為:
我們今天主要講TabNavigator。
效果展示
實(shí)現(xiàn)代碼
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Button, Text, View, Image, StatusBar } from 'react-native'; import { StackNavigator, TabBarBottom, TabNavigator } from "react-navigation"; class Home extends React.Component { static navigationOptions = { tabBarLabel: '熱點(diǎn)', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../res/images/hot_hover.png') : require('../res/images/hot.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>!這是熱點(diǎn)</Text> </View> ); } } class Circle extends React.Component { static navigationOptions = { tabBarLabel: '圈子', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../res/images/coterie.png') : require('../res/images/coterie.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>!這是圈子</Text> </View> ); } } class Tools extends React.Component { static navigationOptions = { tabBarLabel: '工具', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../res/images/tool.png') : require('../res/images/tool.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>!這是工具</Text> </View> ); } } class Mypage extends React.Component { static navigationOptions = { tabBarLabel: '我的', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../res/images/my_hover.png') : require('../res/images/my.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>!這是我的</Text> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', } }); const MyApp = TabNavigator( { Home: { screen: Home, }, Circle: { screen: Circle, }, Tools: { screen: Tools, }, Mypage: { screen: Mypage, }, }, { tabBarOptions: { activeTintColor: '#4BC1D2', inactiveTintColor: '#000', showIcon: true, showLabel: true, upperCaseLabel: false, pressColor: '#823453', pressOpacity: 0.8, style: { backgroundColor: '#fff', paddingBottom: 0, borderTopWidth: 0.5, borderTopColor: '#ccc', }, labelStyle: { fontSize: 12, margin: 1 }, indicatorStyle: { height: 0 }, //android 中TabBar下面會(huì)顯示一條線,高度設(shè)為 0 后就不顯示線了 }, tabBarPosition: 'bottom', swipeEnabled: false, animationEnabled: false, lazy: true, backBehavior: 'none', }); module.exports = MyApp;
配置說明
NavigationOptions
當(dāng)然,通過NavigationOptions來配置我們的tabBarItem:
TabNavigatorConfig
tabBarOptions for (iOS上的默認(rèn)標(biāo)簽欄)TabBarBottom
tabBarOptions for (Android上的默認(rèn)標(biāo)簽欄)TabBarTop
小技巧
1.去掉安卓下的下劃線,設(shè)置:tabBarOptions => indicatorStyle:{ height: 0 };
2.底部導(dǎo)航在導(dǎo)航最上方添加一條分割線,設(shè)置:tabBarOptions => style => borderTopWidth: 0.5, borderTopColor: '#ccc';
3.導(dǎo)航安卓圖標(biāo)和文字間隙比較大,手動(dòng)調(diào)整小設(shè)置:tabBarOptions => labelStyle => margin: 0;
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com