在React Native中选项卡处于活动状态或未处于活动状态时更改选项卡栏图标

时间:2020-01-14 23:09:32

标签: javascript reactjs react-native

我在react native中有一个底部的标签导航,如果一个标签处于活动状态,我希望它显示不同的图标,如果不活动则显示另一个图标。我该如何实现?我的代码在下面

...
import { createBottomTabNavigator } from "react-navigation-tabs";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import HomeScreen from "../screens/HomeScreen";
...

export default createBottomTabNavigator({
 HomePage: {
  screen: HomeScreen,
  navigationOptions: {
    tabBarLabel: "Home",
    tabBarIcon: ({ tintColor }) => (
      <MaterialCommunityIcons
        name='home'
        color={tintColor}
        size={28}
      />
    )
  }
},
....
{
 tabBarOptions: {
  showLabel: false,
  activeTintColor: 'red',
  inactiveTintColor: "grey"
}
}
})

1 个答案:

答案 0 :(得分:1)

经过一些研究,这是我如何做到的。首先将一个“重点突出”的道具传递给tabBarIcon,如下所示。然后进行检查以确定要渲染的图标

export default createBottomTabNavigator({
 HomePage: {
  screen: HomeScreen,
  navigationOptions: {
    tabBarLabel: "Home",
    tabBarIcon: ({ tintColor, focused }) => (
      <MaterialCommunityIcons
        name={focused ? "home" : "home-outline"}
        color={tintColor}
        size={28}
      />
    )
  }
},
....
{
 tabBarOptions: {
  showLabel: false,
  activeTintColor: 'red',
  inactiveTintColor: "grey"
}
}
})

根据上面的代码,无论标签是否处于活动状态,不同的图标都会以淡淡的颜色呈现

相关问题