繁体   English   中英

react-navigation:如何根据当前选项卡更改tabBar颜色

[英]react-navigation: How to change tabBar color based on current tab

我开始使用react-navigation。
更改选项卡时如何更改tabBar背景颜色?

这是一些伪代码,显示了我希望的内容:

_backgroundColor = function() { 
    switch (this.routeName) {
      case 'tabHome':     return { backgroundColor: '#002663' };
      case 'tabRewards':  return { backgroundColor: '#3F9C35' };
      default:            return { backgroundColor: 'white' }           
    }
}

// Tabs setup
export const TabStack = TabNavigator({
  tabHome:     { screen: HomeStack,       },
  tabRewards:  { screen: RewardsStack,    },
}, {
  tabBarOptions: {
    style: _backgroundColor(),
  }
});

在那一刻它总是默认为白色(这很好,因为我的代码是错误的:-)所以我如何传递一些东西 ,在routeName或iconLabel或其他任何东西上触发这个逻辑。

非常感激任何的帮助。
提前致谢。
干杯

import React from 'react';
import { Image, View } from 'react-native';
import { StackNavigator, TabNavigator, TabBarBottom } from 'react-navigation';

const Screen = () => <View />;

const Tabs = TabNavigator(
  {
    Tab1: {
      screen: Screen,
      navigationOptions: {
        title: 'Tab1',
        tabBarIcon: ({ tintColor }) =>
          (<Image
              source={require('../images/iconNotificationNew.png')}
              style={[{ tintColor }]}
          />),
      },
    },
    Tab2: {
      screen: Screen,
      navigationOptions: {
        title: 'Tab2',
        tabBarIcon: ({ tintColor }) =>
          (<Image
              source={require('../images/iconNotificationNew.png')}
              style={[{ tintColor }]}
          />),
      },
    },
    Tab3: {
      screen: Screen,
      navigationOptions: {
        title: 'Tab3',
        tabBarIcon: ({ tintColor }) =>
          (<Image
              source={require('../images/iconNotificationNew.png')}
              style={[{ tintColor }]}
          />),
      },
    },
  },
  {
    lazy: true,
    tabBarComponent: props => {
      const backgroundColor = props.position.interpolate({
        inputRange: [0, 1, 2],
        outputRange: ['orange', 'white', 'green'],
      })
      return (
        <TabBarBottom
          {...props}
          style={{ backgroundColor: backgroundColor }}
        />
      );
    },
    swipeEnabled: true,
    animationEnabled: true,
    tabBarPosition: 'bottom',
    initialRouteName: 'Tab2',
    tabBarOptions: {
      activeTintColor: 'blue',
      inactiveTintColor: 'grey',
    },
  },
);

产量

在Tab2选择上

在Tab1选择上

在Tab3选择上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM