[英]Change tab bar icons when a tab is active or not in React Native
I have a bottom tab navigation in react native that I want it to display different icon if a tab is active and another icon if it is not active.我在 React Native 中有一个底部选项卡导航,我希望它在选项卡处于活动状态时显示不同的图标,如果未处于活动状态,则显示另一个图标。 How do I achieve it?
我如何实现它? I have my code below
我在下面有我的代码
...
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"
}
}
})
After some research here's how I managed to do it.经过一些研究,这是我设法做到的。 First pass a 'focused' prop to tabBarIcon like below.
首先将一个“聚焦”道具传递给 tabBarIcon,如下所示。 Then do checks to determine which icon to render
然后进行检查以确定要呈现的图标
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"
}
}
})
Based on the code above, different icons are rendered with tint color whether the tab is active on not根据上面的代码,无论选项卡是否处于活动状态,不同的图标都会以淡色呈现
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.