这就是我将图标更改为唯一活动标签的方法:
const TabNavigator = createBottomTabNavigator(
{
Home: AppStack,
Notification: Notifications,
Account: SettingsScreen,
},
{
defaultNavigationOptions: ({navigation}) => ({
tabBarIcon: ({focused, tintColor}) =>
getTabBarIcon(navigation, focused, tintColor),
}),
tabBarOptions: {
activeTintColor: colors.tealC,
inactiveTintColor: 'gray',
},
},
);
我创建了bottomTabnavigator并调用了getTabBarIcon函数,
//这个函数在标签被选中时给出图标
const getTabBarIcon = (navigation, focused, tintColor) => {
const {routeName} = navigation.state;
if (routeName === 'Home') {
if (focused) {
return (
<Image
style={homeStyles.bottomTabI}
source={require('./app/assets/images/homeF.png')}
/>
);
} else {
return (
<Image
style={homeStyles.bottomTabI}
source={require('./app/assets/images/unstar.png')}
/>
);
}
}
if (routeName === 'Notification') {
if (focused) {
return (
<Image
style={homeStyles.bottomTabI}
source={require('./app/assets/images/whatsapp.png')}
/>
);
} else {
return (
<Image
style={homeStyles.bottomTabI}
source={require('./app/assets/images/bellNF.png')}
/>
);
}
}
if (routeName === 'Account') {
if (focused) {
return (
<Image
style={homeStyles.bottomTabI}
source={require('./app/assets/images/whatsapp.png')}
/>
);
} else {
return (
<Image
style={homeStyles.bottomTabI}
source={require('./app/assets/images/profileNF.png')}
/>
);
}
}
};
所以这里基本上路由名称首先检查它是否是那个路由名称,如果它是那个,然后根据 if(focused) 我添加图标。希望您可以在选择活动选项卡时对样式执行相同的操作。
希望对您有所帮助。如有疑问,请随意。