1.安装依赖

yarn add react-navigation-tabs

2.第二步引入配置路由

在路由文件里 新建一个tabs.js

  1. import { createBottomTabNavigator } from 'react-navigation-tabs';
  2. import MusicPage from '../views/tabs/MusicPage';
  3. import Mvpage from '../views/tabs/Mvpage';
  4. const TabNavigator = createBottomTabNavigator({
  5. Music:{
  6. screen:MusicPage,
  7. navigationOptions:{
  8. tabBarLabel:"音乐"
  9. }
  10. },
  11. Mv:Mvpage
  12. });
  13. export default TabNavigator

3.新建底部导航需要的页面

image.png
使用

4在父组件中使用

  1. import React, { Component } from 'react';
  2. import Tabs from '../routers/tabs'
  3. export default class HomePage extends Component {
  4. render() {
  5. return <Tabs/>
  6. }
  7. }

5.直接使用(第二种)路由文件

//tabs.js

  1. import { createBottomTabNavigator } from 'react-navigation-tabs';
  2. import MusicPage from '../views/tabs/MusicPage';
  3. import Mvpage from '../views/tabs/Mvpage';
  4. const TabNavigator = createBottomTabNavigator({
  5. Music:{
  6. screen:MusicPage,
  7. navigationOptions:{
  8. tabBarLabel:"音乐"
  9. }},
  10. Mv:Mvpage
  11. });
  12. export default TabNavigator //直接导出

//index.js

  1. import Tabs from './tabs'
  2. const MainNavigator = createStackNavigator({
  3. HomePage:{
  4. screen:Tabs
  5. }
  6. })

6.配置颜色等

  1. import { createBottomTabNavigator } from 'react-navigation-tabs';
  2. import MusicPage from '../views/tabs/MusicPage';
  3. import Mvpage from '../views/tabs/Mvpage';
  4. const TabNavigator = createBottomTabNavigator({
  5. Music:{
  6. screen:MusicPage,
  7. navigationOptions:{
  8. tabBarLabel:"音乐"
  9. }
  10. },
  11. Mv:Mvpage
  12. },{
  13. tabBarOptions:{
  14. activeTintColor:'red', //被选中时颜色
  15. inactiveTintColor:"blue", //未被选中时的颜色
  16. style:{
  17. backgroundColor:'#4442' //背景颜色
  18. },
  19. labelStyle:{
  20. fontSize:18 //字体大小
  21. }
  22. }
  23. });
  24. export default TabNavigator

7.配置带图标的底部导航

安装依赖
yarn add react-native-vector-icons 参考icons的使用这节笔记

  1. import React from 'react';
  2. import Ionicons from 'react-native-vector-icons/Ionicons'
  3. //首先要引入 加在颜色配置的大括号里
  4. defaultNavigationOptions: ({ navigation }) => ({
  5. tabBarIcon: ({ focused, horizontal, tintColor }) => {
  6. const { routeName } = navigation.state;
  7. let IconComponent = Ionicons;
  8. let iconName;
  9. if (routeName === 'Music') {
  10. iconName = `ios-home`;
  11. } else if (routeName === 'Mv') {
  12. iconName = `ios-options`;
  13. }
  14. return <IconComponent name={iconName} size={25} color={tintColor} />;
  15. },
  16. }),

8.底部导航对应的顶部导航

  1. //首先要引入 加在颜色配置的大括号里
  2. navigationOptions:({navigation})=>{
  3. const {routeName} = navigation.state.routes[navigation.state.index];
  4. if (routeName === 'Music') {
  5. return {
  6. headerTitle:"音乐"
  7. }
  8. } else if (routeName === 'Mv') {
  9. return {
  10. headerTitle:"Mv"
  11. }
  12. }
  13. }

9.完整事例

  1. import React from 'react';
  2. import { createBottomTabNavigator } from 'react-navigation-tabs';
  3. import MusicPage from '../views/tabs/MusicPage';
  4. import Mvpage from '../views/tabs/Mvpage';
  5. import Ionicons from 'react-native-vector-icons/Ionicons'
  6. const TabNavigator = createBottomTabNavigator({
  7. Music:{
  8. screen:MusicPage,
  9. navigationOptions:{
  10. tabBarLabel:"音乐"
  11. }
  12. },
  13. Mv:Mvpage
  14. },
  15. {
  16. defaultNavigationOptions: ({ navigation }) => ({
  17. tabBarIcon: ({ focused, horizontal, tintColor }) => {
  18. const { routeName } = navigation.state;
  19. let IconComponent = Ionicons;
  20. let iconName;
  21. if (routeName === 'Music') {
  22. iconName = `ios-home`;
  23. } else if (routeName === 'Mv') {
  24. iconName = `ios-options`;
  25. }
  26. return <IconComponent name={iconName} size={25} color={tintColor} />;
  27. },
  28. }),
  29. tabBarOptions:{
  30. activeTintColor:'red',
  31. inactiveTintColor:"blue",
  32. style:{
  33. backgroundColor:'#4442'
  34. },
  35. labelStyle:{
  36. fontSize:18
  37. }
  38. },
  39. navigationOptions:({navigation})=>{
  40. const {routeName} = navigation.state.routes[navigation.state.index];
  41. if (routeName === 'Music') {
  42. return {
  43. headerTitle:"音乐"
  44. }
  45. } else if (routeName === 'Mv') {
  46. return {
  47. headerTitle:"Mv"
  48. }
  49. }
  50. }
  51. });
  52. export default TabNavigator