che*_*ole 2 javascript reactjs 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"
}
}
})
Run Code Online (Sandbox Code Playgroud)
经过一番研究,我是如何做到这一点的。首先将 'focused' 属性传递给 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"
}
}
})
Run Code Online (Sandbox Code Playgroud)
根据上面的代码,无论选项卡是否处于活动状态,都会使用色调颜色呈现不同的图标
| 归档时间: |
|
| 查看次数: |
8629 次 |
| 最近记录: |