如何动态更新react-navigation标签导航器的activeTintColor?

时间:2019-05-11 05:41:23

标签: javascript react-native react-navigation

我想动态更新标签栏导航器的颜色,这是我的代码:

const MyStack = createBottomTabNavigator({ ... }, {
  tabBarOptions: {
    activeTintColor: "green",
  }
});

class CustomNavigator extends React.Component {
  static router = MyStack.router;
  render() {
    const { navigation } = this.props;

    return <MyStack
      navigation={navigation}
      activeTintColor={"red"}
    />;
  }
}

标签栏的颜色始终为“绿色”,而activeTintColor={"red"}无效。我也尝试过这个(也不行):

return <MyStack
          navigation={navigation}
          navigatorOptions={{
              tabBarOptions: {
                  activeTintColor: "red",
              }
          }}
        />

谢谢。

2 个答案:

答案 0 :(得分:0)

将变量传递给您的孩子。

const MyStack = createBottomTabNavigator({ ... }, {
  tabBarOptions: {[
    {
    activeTintColor: "green",
},
this.props.tabBarOptions
  ]}
});

class CustomNavigator extends React.Component {
  static router = MyStack.router;
  render() {

    return <MyStack
      activeTintColor={"red"}
    />;
  }
}

答案 1 :(得分:0)

您可以在屏幕上用navigationOptions覆盖颜色。

class Home extends React.Component {
  static navigationOptions = {
    tabBarOptions: {
      activeTintColor: 'red', // overwrite the default green color
    },
  };

  render() {
    return (
      <View style={styles.container}>
        <Text>Home</Text>
      </View>
    );
  }
}

Demo