【问题标题】:React-Native How to change the overall background color of the Tabbar on the focused other tabsReact-Native 如何更改焦点其他选项卡上选项卡栏的整体背景颜色
【发布时间】:2020-09-03 08:39:14
【问题描述】:

我想更改标签栏的背景颜色。

我在底部导航中有 5 个标签。 首先,当我触摸主页选项卡时。我想更改 Tabbar 的背景颜色为“透明” 第二,当我触摸其他(四)选项卡时。我想更改 Tabbar 的背景颜色为“白色” 我也想通过其他选项卡更改 activeTintColor。

这是我的代码和屏幕截图(我想做这个屏幕截图)。

Now my home screen. Home Screen shot

I want this other screen. screen shot


import { View } from 'react-native';

import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

import MyHomeStack from './HomeStack';
import MyProfileStack from './ProfileStack';

import InformScreen from '../screens/InformScreen';
import SearchScreen from '../screens/SearchScreen';
import UploadScreen from '../screens/UploadScreen';

import CustomIcon from '../components/CustomIcon';

const Tab = createBottomTabNavigator();

function MainTabStack(){
  return (
    <Tab.Navigator
      initialRouteName="Home"
      labelStyle={{ fontSize: 12 }}
      tabBarOptions={{
        style: {
          height: '9%',
          backgroundColor: 'transparent',
          position:'absolute',
          bottom:0,
          elevation:0
        },
        activeTintColor: '#000000',
        showLabel: false,
      }}>
      <Tab.Screen
        name="Home"
        component={MyHomeStack}
        options={{
          tabBarIcon: ({ color, size }) => (
            <CustomIcon name="nav_home" color={color} size={size}/>
          )
        }}
      />
      <Tab.Screen
        name="Search"
        component={SearchScreen}
        options={{
          tabBarColor:'red',

          tabBarIcon: ({ color, size }) => (
            <CustomIcon name="nav_store" color={color} size={size} />
          ),
        }}
      />
      <Tab.Screen
        name="Upload"
        component={UploadScreen}
        options={{
          tabBarIcon: ({ color, size }) => (
            <CustomIcon name="nav_upload" color={color} size={28} />
          ),
        }}
        listeners={({ navigation }) => ({
          tabPress: event => {
            event.preventDefault();
            navigation.navigate('UploadScreen');
          },
        })}
      />
      <Tab.Screen
        name="Inform"
        component={InformScreen}
        options={{
          tabBarIcon: ({ color, size }) => (
            <CustomIcon name="nav_closet" color={color} size={size} />
          ),
        }}
      />
      <Tab.Screen
        name="mypage"
        component={MyProfileStack}
        options={{
          tabBarIcon: ({ color, size }) => (
            <CustomIcon name="mypage" color={color} size={size} />
          ),
        }}
      />


    </Tab.Navigator>
  );
}

export default MainTabStack;

【问题讨论】:

    标签: react-native react-native-navigation react-navigation-bottom-tab


    【解决方案1】:

    在标签栏选项中,您是否尝试过将背景颜色动态更改为所需的颜色。

    constructor(props){
        this.tabIndex=0;
    } 
    
    <Tab.Navigator
        initialRouteName="Home"
        labelStyle={{ fontSize: 12 }}
        screenOptions={({ route }) => ({
            if (route.name === 'Home') {
              this.tabIndex=4;
            } else if (route.name === 'Settings') {
              this.tabIndex=5;
            }
        })}
        tabBarOptions={{
            style: {
            height: '9%',
            backgroundColor: this.tabIndex==4?"#fff":"transparent",
            //backgroundColor: 'transparent',
            position:'absolute',
            bottom:0,
            elevation:0
            },
            activeTintColor: '#000000',
            showLabel: false,
        }}>
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
    </Tab.Navigator>
    

    【讨论】:

    • 谢谢 Vijay122!我需要更多问题。
    • 我在哪里可以添加那个代码???找不到变量:tabindex... T.T
    • 嗨,这是从screenOptions派生的。我已根据您的情况对其进行了修改。如果这不起作用,请告诉我。
    • Vijay122~ 我了解您的代码逻辑。但是我的代码是功能代码...所以我不知道如何添加构造函数(道具)...您知道如何添加该代码吗?而且,我添加 ({route})=> ... 但它有错误 T,T
    • Oh Vijay122 我申请成功了!!谢谢!祝你有美好的一天~~~~~
    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2018-08-24
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多