How to change the color of the active / selected tab?

北城以北 提交于 2021-02-08 05:21:33

问题


I want the color to be the default gray color when the tab is not selected but to be my tabBarColor color when the tab is selected. I could not find a way to change the color of the title in the tab bar.

How can I do that?

This is my code:

Home:{
  screen: TabNavigator({
   Home: {
    screen: HomeScreen,
    navigationOptions: ({ navigation }) => ({
      title: 'Home',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
        name={focused ? 'ios-home' : 'ios-home-outline'}
        size={26}
        style={{ color: focused ? `${tabBarColor}` : tintColor}}
        />
      ),
      //headerStyle: {backgroundColor: "#553A91"},
      //headerTitleStyle: {color: "#FFFFFF"},
      header: null,
    }),
  },
  Profile: {
    screen: ProfileScreen,
    navigationOptions: ({ navigation }) => ({
      title: 'Profile',
      tabBarIcon: ({ tintColor, focused }) => (
        <Ionicons
        name={focused ? 'ios-people' : 'ios-people-outline'}
        size={26}
        style={{ color: focused ? `${tabBarColor}` : tintColor }}
        />
      ),
      //headerStyle: {backgroundColor: "#553A91"},
      //headerTitleStyle: {color: "#FFFFFF"},
      header: null,
    }),
  },
}),
}

回答1:


In TabNavigator Docs, it is clearly indicated that you need to use activeTintColor

activeTintColor: label and icon color of the active tab

Example:

const MyApp = TabNavigator({
  Home: {
    screen: MyHomeScreen,
  },
  Notifications: {
    screen: MyNotificationsScreen,
  },
}, {
  navigationOptions: ({navigation}) => ({
        tabBarIcon: ({focused}) => {
            ...
        }
  }),
  tabBarOptions: {
        activeTintColor: '#ffffff',
  },
});



回答2:


  1. Define variable route,
  2. Add props listeners to each <Tab.Screen>,
  3. Use variable route for change color of tabBarLabel.

Example :

const [route, setRoute] = useState('home');
 

        <Tab.Navigator>
           <Tab.Screen listeners={{
                  tabPress: (e) => {
                    setRoute('home');
                  },
                }}
                options={{
                  tabBarLabel: (
                    <Text
                      style={{
                        color: route === 'home' ? 'ACTIVE_COLOR' : 'INACTIVE_COLOR',
                      }}>
                      home
                    </Text>
                  ) />
    
    <Tab.Screen listeners={{
                  tabPress: (e) => {
                    setRoute('profile');
                  },
                }}
                options={{
                  tabBarLabel: (
                    <Text
                      style={{
                        color: route === 'profile' ? 'ACTIVE_COLOR' : 'INACTIVE_COLOR',
                      }}>
                      profile
                    </Text>
                  ) />
    </Tab.Navigator>

I hope I was able to help you



来源:https://stackoverflow.com/questions/45513268/how-to-change-the-color-of-the-active-selected-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!