React Native中的TabBarBottom不显示图标/图像

时间:2017-04-22 04:59:30

标签: react-native react-navigation

我几乎从TabNavigator文档中获取了示例代码,图标/图像根本不会出现在iOS或Android上。即使标签覆盖也似乎没有生效。我做错了什么?

https://reactnavigation.org/docs/navigators/tab

以下是我一直使用的文档的链接: enter image description here

这是我的代码:

class MyHomeScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Not displayed',
    // Note: By default the icon is only shown on iOS. Search the showIcon option below.
    tabBarIcon: ({ tintColor }) => (
      <Image
        source={require('./chats-icon.png')}
        style={[styles.icon, {tintColor: tintColor}]}
      />
    ),
  };

  render() {
    return (
      <Button
        onPress={() => this.props.navigation.navigate('Notifications')}
        title="Go to notifications"
      />
    );
  }
}

class MyNotificationsScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Notifications',
    tabBarIcon: ({ tintColor }) => (
      <Image
        source={require('./notif-icon.png')}
        style={[styles.icon, {tintColor: tintColor}]}
      />
    ),
  };

  render() {
    return (
      <Button
        onPress={() => this.props.navigation.goBack()}
        title="Go back home"
      />
    );
  }
}

const styles = StyleSheet.create({
  icon: {
    width: 26,
    height: 26,
  },
});

const MyApp = TabNavigator({
  Displayed: {
    screen: MyHomeScreen,
  },
  Notifications: {
    screen: MyNotificationsScreen,
  },
}, {
  tabBarOptions: {
    activeTintColor: '#e91e63',
  },
});

4 个答案:

答案 0 :(得分:7)

好吧,在我想把脸撞到键盘上后,我终于明白了。

标题栏和标签栏图标实际上与文档内部的结构不同。

  const MyApp = TabNavigator({
    Displayed: {
      screen: MyHomeScreen,
      navigationOptions: {
          title: 'Favorites',
          tabBar: {
            icon: ({tintColor}) => (<Image
              source={require('./chats-icon.png')}
              style={{width: 26, height: 26, tintColor: tintColor}}
            />)
          },
      },
    },
    ...

 class MyHomeScreen extends React.Component {
    static navigationOptions = {
        title: 'Foo Bar',
        tabBar: {
            icon: ({ tintColor }) => (
              <Image
                source={require('./chats-icon.png')}
                style={{width: 26, height: 26, tintColor: tintColor}}
              />
            ),
        }
      };
 ...

答案 1 :(得分:1)

我在文档本身中搜索堆栈溢出时的答案。用于在本机底部选项卡中将图像用作图标。这是根据当前的React Navigation 4.x。

const tabNavigator = createBottomTabNavigator({
        Style: HomeScreen,
        Location: LocationScreen,   
    },
        {
            defaultNavigationOptions: ({ navigation }) => ({
                tabBarIcon: ({ focused, horizontal, tintColor }) => {
                    const { routeName } = navigation.state;
                    if (routeName === 'Style') {
                        return <Image
                            source={require('./screens/assets/notifications.png')}
                            style={{ height: 25, width: 25, tintColor: tintColor }}
                        />;
                    } else if (routeName === 'Location') {
                        return <Image
                            source={require('./screens/assets/location.png')}
                            style={{ height: 35, width: 35, tintColor: tintColor }}
                        />;
                    }
                },
            }),

        tabBarOptions: {
            activeTintColor: 'tomato', //For changing tint colors
            inactiveTintColor: 'gray',
        },
    }
),

您可以找到其他信息hereHere is my bottom tabs navigator with image

答案 2 :(得分:0)

遇到同样的问题,但这个解决方案对我没用。无效的密钥标签栏&#39;在导航选项中定义...... 编辑: 当我从tab选项卡导航器中删除tabBarOptions时,它已经工作了。 而是使用activeTintColor和inactiveTintColor作为TabBarBottom的道具。

答案 3 :(得分:0)

对于react-navigation-tabs v2.6.2,现在是described in the doc

要更新旧示例,您必须将tabBar: { icon: ... }替换为tabBarIcon: ...

例如

 class MyHomeScreen extends React.Component {
    static navigationOptions = {
        title: 'Foo Bar',
        tabBarIcon: ({ tintColor }) => (
              <Image
                source={require('./chats-icon.png')}
                style={{width: 26, height: 26, tintColor: tintColor}}
              />
            )
      };