I’m using Material Top Tabs Navigator from React Navigation. Here’s an example:
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
const Tab = createMaterialTopTabNavigator();
function MyTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Chat" component={ChatScreen} />
<Tab.Screen name="Contacts" component={ContactsScreen} />
<Tab.Screen name="Albums" component={AlbumsScreen} />
</Tab.Navigator>
);
}
By default, tab indicator is displayed only for the active tab like the following:
However I want inactive tabs to have tab indicators as well. These inactive tabs indicators should be a different color like below:
I can change default indicator color for active tab by adding tabBarIndicatorStyle
option to navigator:
<Tab.Navigator
screenOptions={{ tabBarIndicatorStyle: { backgroundColor: colors.white } }}
>
<Tab.Screen name="Chat" component={ChatScreen} />
<Tab.Screen name="Contacts" component={ContactsScreen} />
<Tab.Screen name="Albums" component={AlbumsScreen} />
</Tab.Navigator>
But how to set indicator color for inactive tabs?
2
Answers
To set the style of the indicator under the inactive tab, you need to use a custom tab bar component because the default createMaterialTopTabNavigator does not support directly styling the indicator for inactive tabs. You can achieve this by customizing the tab bar’s rendering.
Here’s an example of how you can create a custom tab bar component to style the indicator under the inactive tabs:
you can use custom tab bar like this:
In @react-navigation/material-top-tabs, there isn’t a direct prop for setting the inactive indicator color.
To achieve different styles for active and inactive tab indicators, you would need a custom tab bar component. Here’s how you can create a custom tab bar to handle different indicator colors for active and inactive tabs:
Create a custom tab bar component: