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

时间:2017-03-20 17:48:50

标签: javascript react-native 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或其他任何东西上触发此逻辑。< / p>

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

1 个答案:

答案 0 :(得分:8)

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',
    },
  },
);

<强> 输出

on Tab2 Selection

on Tab1 Selection

on Tab3 Selection