[英]How can I override tabBarOptions and change the color of the navigation icons?
How can I change the colors of the icons that are active by removing the 'tabBarOptions' part and everything still works?如何通过删除“tabBarOptions”部分来更改活动图标的 colors 并且一切仍然有效?
Going through an app I made some time ago in a tutorial, I came across this warning in the console:浏览我前段时间在教程中制作的应用程序时,我在控制台中遇到了这个警告:
Bottom Tab Navigator: 'tabBarOptions'
is deprecated.底部选项卡导航器:
'tabBarOptions'
已弃用。 Migrate the options to 'screenOptions'
instead.将选项迁移到
'screenOptions'
。 Place the following in 'screenOptions' in your code to keep current behavior:将以下内容放在代码的“screenOptions”中以保持当前行为:
Reading the information that React Navigation offers about Bottom Tabs Navigation , I have managed to solve the error in the following way.阅读React Navigation 提供的关于 Bottom Tabs Navigation的信息,我设法通过以下方式解决了错误。
<Tab.Screen
name="restaurants"
component={RestaurantsStack}
options={{
title: "Restaurants",
headerShown: false,
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="magnify"
color={color}
size={size}
/>
),
}}
/>
However, in my application, I want to change the color of the buttons when we navigate through the screens, the icon of the screen we are on is shown in a different color, and at the time the code was the one shown below, and it is That's why it shows me the warning.但是,在我的应用程序中,我想在浏览屏幕时更改按钮的颜色,我们所在屏幕的图标以不同的颜色显示,当时代码如下所示,并且这就是为什么它向我显示警告。 The problem is that I don't know how I can correct this
问题是我不知道该如何纠正
This is the full code of my file这是我文件的完整代码
const Tab = createBottomTabNavigator()
export default function Navigation() {
const screenOptions = (route, color) => {
let iconName
switch (route.name) {
case "tiendas":
iconName = "compass-outline"
break;
case "favorites":
iconName = "heart-outline"
break;
case "top-tiendas":
iconName = "star-outline"
break;
case "search":
iconName = "magnify"
break;
case "account":
iconName = "home-outline"
break;
}
return (
<Icon
type="material-community"
name={iconName}
size={22}
color={color}
/>
)
}
return (
<NavigationContainer>
<Tab.Navigator
initialRouteName="tiendas"
tabBarOptions={{
inactiveTintColor: "#f48b28",
activeTintColor: "#633204"
}}
screenOptions={({ route }) => ({
tabBarIcon: ({ color }) => screenOptions(route, color)
})}
>
<Tab.Screen
name="tiendas"
component={tiendasStack}
options={{
title: "Tiendas",
headerShown: false
}}
/>
<Tab.Screen
name="favorites"
component={FavoritesStack}
options={{
title: "Favoritos",
headerShown: false
}}
/>
<Tab.Screen
name="top-tiendas"
component={ToptiendasStack}
options={{
title: "Top 10",
headerShown: false,
}}
/>
<Tab.Screen
name="search"
component={SearchStack}
options={{
title: "Buscar",
headerShown: false,
}}
/>
<Tab.Screen
name="account"
component={AccountStack}
options={{
title: "Cuenta",
headerShown: false,
}}
/>
</Tab.Navigator>
</NavigationContainer>
)
}
As I already said, I could solve it in the following way, but I don't know how to add the desired color and how to make it change when the icon is active:正如我已经说过的,我可以通过以下方式解决它,但我不知道如何添加所需的颜色以及如何在图标处于活动状态时更改它:
This removes the warning, but I can't change the colors: How can I change the colors of the icons that are active by removing the 'tabBarOptions'
part and everything still works?这会删除警告,但我无法更改 colors:如何通过删除
'tabBarOptions'
部分更改活动图标的 colors 并且一切仍然有效?
const Tab = createBottomTabNavigator()
export default function Navigation() {
// Navigation buttons
return (
<NavigationContainer>
<Tab.Navigator >
<Tab.Screen
name="tiendas"
component={TiendasStack}
options={{
title: "Tiendas",
headerShown: false,
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="magnify"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name="favorites"
component={FavoritesStack}
options={{
title: "Favoritos",
headerShown: false,
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="heart-outline"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name="top-tiendas"
component={TopTiendasStack}
options={{
title: "Top 5",
headerShown: false,
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="star-outline"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name="search"
component={SearchStack}
options={{
title: "Buscar",
headerShown: false,
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="magnify"
color={color}
size={size}
/>
),
}}
/>
<Tab.Screen
name="account"
component={AccountStack}
options={{
title: "Cuenta",
headerShown: false,
tabBarIcon: ({ color, size }) => (
<MaterialCommunityIcons
name="home-outline"
color={color}
size={size}
/>
),
}}
/>
</Tab.Navigator>
</NavigationContainer>
)
}
if you want to change only Icons color, not text color, then you can use 'focused' in tabBarIcon.如果你只想改变图标颜色,而不是文本颜色,那么你可以在 tabBarIcon 中使用'focused'。
tabBarIcon: ({focused, color, size }) => (
<MaterialCommunityIcons
name="magnify"
color={focused ? focusedColor : color}
size={size}
/>
)
Add tabBarInactiveTintColor and tabBarActiveTintColor options to screenOptions like that:像这样向 screenOptions 添加 tabBarInactiveTintColor 和 tabBarActiveTintColor 选项:
<Tab.Navigator
initialRouteName="tiendas"
screenOptions={({ route }) => ({
tabBarInactiveTintColor: "#f48b28",
tabBarActiveTintColor: "#633204"
})}
>...</Tab.Navigator>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.