0Pricing
React Native Academy · 课时

底部标签导航器

创建包含三个标签的底部标签导航器,使用矢量图标库自定义每个标签的图标,并配置活动标签的着色。

底部标签导航器 是 CoddyKit 上的免费 React Native Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 React Native Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 React Native Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What is a Bottom Tab Navigator?

A bottom tab navigator renders a tab bar at the bottom of the screen with one tab per registered screen. Tapping a tab switches to that screen. This pattern is very common in mobile apps (Instagram, Twitter, Airbnb) and is provided by the @react-navigation/bottom-tabs package.

Installing the Bottom Tabs Package

Install the bottom tabs navigator package separately from the React Navigation core. If you are using Expo, also make sure react-native-screens and react-native-safe-area-context are already installed from the previous setup step.

npm install @react-navigation/bottom-tabs

Creating the Tab Navigator

Call createBottomTabNavigator() to get a Tab object with Tab.Navigator and Tab.Screen components. Register screens exactly as you would in a stack navigator. Each Tab.Screen name becomes the default tab label.

import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

const Tab = createBottomTabNavigator();

function MyTabs() {
  return (
    <Tab.Navigator>
      <Tab.Screen name='Home' component={HomeScreen} />
      <Tab.Screen name='Search' component={SearchScreen} />
      <Tab.Screen name='Profile' component={ProfileScreen} />
    </Tab.Navigator>
  );
}

Adding Icons to Tab Screens

Each tab should have an icon to help users recognize it quickly. Use tabBarIcon inside the screen's options prop. The render function receives a focused boolean and a color so you can render different icons or colors for active vs inactive tabs.

import { Ionicons } from '@expo/vector-icons';

<Tab.Screen
  name='Home'
  component={HomeScreen}
  options={{
    tabBarIcon: ({ focused, color, size }) => (
      <Ionicons
        name={focused ? 'home' : 'home-outline'}
        size={size}
        color={color}
      />
    ),
  }}
/>

Customizing Active and Inactive Colors

Set tabBarActiveTintColor and tabBarInactiveTintColor in screenOptions to control the color of tab icons and labels for the selected and unselected states. These color values are passed into the tabBarIcon render function automatically.

<Tab.Navigator
  screenOptions={{
    tabBarActiveTintColor: '#6200ee',
    tabBarInactiveTintColor: '#888',
  }}
>

Hiding the Tab Bar on Specific Screens

Sometimes you want to hide the tab bar when navigating to a detail screen. Set tabBarStyle: { display: 'none' } in the screen's options or use the navigation prop to toggle it dynamically with navigation.setOptions.

<Tab.Screen
  name='Details'
  component={DetailsScreen}
  options={{ tabBarStyle: { display: 'none' } }}
/>

Changing Tab Label and Badge

Override the tab label with tabBarLabel in the screen options. Add a notification badge using tabBarBadge to show a count indicator on the icon — useful for messaging or notification tabs. Set tabBarBadge to a number or string.

<Tab.Screen
  name='Messages'
  component={MessagesScreen}
  options={{
    tabBarLabel: 'Inbox',
    tabBarBadge: 3,
  }}
/>

Styling the Tab Bar Background

Customize the tab bar background color and border using tabBarStyle in screenOptions. You can set backgroundColor, borderTopColor, elevation (Android shadow), and height to match your design system.

<Tab.Navigator
  screenOptions={{
    tabBarStyle: {
      backgroundColor: '#1a1a2e',
      borderTopColor: '#333',
      height: 60,
    },
    tabBarLabelStyle: { fontSize: 12, paddingBottom: 4 },
  }}
>

Navigating Between Tabs Programmatically

Use navigation.navigate('TabName') to switch to a specific tab from any screen within the navigator. This is useful when a button action in one tab should take the user to another section of the app. The active tab's state is preserved.

function HomeScreen({ navigation }) {
  return (
    <Button
      title='Go to Profile'
      onPress={() => navigation.navigate('Profile')}
    />
  );
}

Listening to Tab Press Events

Use the tabPress event on navigation to intercept a tab press. This is useful for scrolling a list back to the top when the user taps the active tab. Call e.preventDefault() if you want to handle the press yourself without the default navigation action.

navigation.addListener('tabPress', (e) => {
  // Prevent default behavior
  e.preventDefault();
  // Custom action: scroll to top, show modal, etc.
});

Nesting a Stack Inside a Tab

In real apps each tab often contains its own stack of screens. Simply nest a Stack.Navigator as the component for a Tab.Screen. The stack renders inside the tab area while the tab bar remains visible at the bottom of the screen.

function HomeStack() {
  return (
    <Stack.Navigator>
      <Stack.Screen name='Home' component={HomeScreen} />
      <Stack.Screen name='Post' component={PostScreen} />
    </Stack.Navigator>
  );
}

<Tab.Screen name='HomeTab' component={HomeStack} />

Quick Check

Test your understanding of Bottom Tab Navigator concepts from this lesson.

Lesson Recap

In this lesson you learned: install and create a bottom tab navigator with createBottomTabNavigator, add icons using tabBarIcon in screen options, and customize colors, labels, and badges globally via screenOptions. Next up we explore the Drawer Navigator and nested navigators.

常见问题解答

「底部标签导航器」课时是免费的吗?

是的 — 「底部标签导航器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 React Native Academy 课程的其余内容,请升级到 CoddyKit PRO。 React Native Academy 课程共包含 4 节课。

「底部标签导航器」这节课中我会学到什么?

创建包含三个标签的底部标签导航器,使用矢量图标库自定义每个标签的图标,并配置活动标签的着色。 你通过在浏览器中直接运行的动手代码来练习 React Native Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 React Native Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 React Native Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「底部标签导航器」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 React Native Academy 课中编写并运行代码吗?

能。每节 React Native Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 安装并配置 React Navigation
  2. 在屏幕之间导航并传递参数
  3. 底部标签导航器
  4. 抽屉导航器与嵌套导航器
← 返回 React Native Academy