You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native顶部标签导航背景色与图标样式设置求助

Fixing React Native Material Top Tab Navigator Styling & Icons

Hey there! Let's get your top tab navigation looking exactly how you want it. I noticed a few key issues in your code that are preventing your styles from applying, plus I'll walk you through adding icons to your tabs step by step.

First, Fix the Background Color Issue

You’re trying to set tab styling properties like style and backgroundColor inside individual Tab.Screen options, but for createMaterialTopTabNavigator, these global tab bar styles need to be set in the Tab.Navigator's screenOptions instead. Also, you had a small typo in your inactiveTintColor (missing the # at the start of the hex code).

Next, Add Tab Icons

To add icons, use the tabBarIcon prop in each screen's options. This prop takes a function that receives a focused parameter, so you can show different icons for active and inactive tab states.

Here's Your Updated Working Code

import "react-native-gesture-handler"
import React from "react"
import { DefaultTheme } from "@react-navigation/native"
import { AppearanceProvider, useColorScheme } from "react-native-appearance"
import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs"
import { PersonalSetupScreen } from "./tabs/personal-setup"
import { CompanySetupScreen } from "./tabs/company-setup"
import { Images } from "../../config"
// Don't forget to import Image from react-native
import { Image } from "react-native"

const Tab = createMaterialTopTabNavigator()

const MyDarkTheme = {
  dark: true,
  colors: {
    primary: "rgb(255, 255, 255)",
    background: "rgb(33, 20, 122)",
    card: "rgb(255, 255, 255)",
    text: "rgb(255, 255, 255)",
    border: "rgb(199, 199, 204)",
    notification: "rgb(255, 69, 58)",
  },
}

export default function HomeTabs() {
  const scheme = useColorScheme()
  return (
    <Tab.Navigator
      // Global tab bar styles live here
      screenOptions={{
        tabBarStyle: { backgroundColor: "#21147a" },
        tabBarActiveTintColor: "#ffffff", // White text for active tab
        tabBarInactiveTintColor: "#cccccc", // Light gray for inactive tab
        tabBarIndicatorStyle: { backgroundColor: "#ffffff" }, // White underline for active tab
      }}
    >
      <Tab.Screen
        name="PersonalSetup"
        component={PersonalSetupScreen}
        options={{
          tabBarLabel: "Personal Details",
          // Add icon with active/inactive state
          tabBarIcon: ({ focused }) => (
            <Image
              source={focused ? Images.personalActive : Images.personalInactive}
              style={{ width: 24, height: 24 }}
            />
          ),
        }}
      />
      <Tab.Screen
        name="CompanySetup"
        component={CompanySetupScreen}
        options={{
          tabBarLabel: "Company Details",
          tabBarIcon: ({ focused }) => (
            <Image
              source={focused ? Images.companyActive : Images.companyInactive}
              style={{ width: 24, height: 24 }}
            />
          ),
        }}
      />
    </Tab.Navigator>
  )
}

Key Changes Explained

  1. Moved Global Styles to Navigator: All tab bar-wide styles (background color, tint colors, active tab indicator) are now in Tab.Navigator's screenOptions — this is where createMaterialTopTabNavigator expects these settings to live.
  2. Fixed Color Typo: Corrected inactiveTintColor to include the required # prefix for hex codes.
  3. Added Tab Icons: Used tabBarIcon with a function that switches between active/inactive icons based on the focused state. Just make sure your Images config has the correct paths for your icon assets.
  4. Cleaned Up Redundant Props: Removed style props from individual screens since they don’t apply there for this navigator type.

Quick Extra Tips

  • If you want to adjust spacing between icons and labels, add tabBarLabelStyle in the navigator's screenOptions (e.g., marginLeft: 8).
  • Tweak tabBarIndicatorStyle to change the active tab underline’s color, height, or thickness to match your design.

内容的提问来源于stack exchange,提问作者MIike Eps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 20:09:05