13

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

在此处输入图像描述

这是我一直在使用的文档的链接: https ://reactnavigation.org/docs/navigators/tab

这是我的代码:

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

4 回答 4

11

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

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

  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}}
              />
            ),
        }
      };
 ...
于 2017-04-22T22:33:24.587 回答
2

至于react-navigation-tabsv2.6.2 现在是否如文档中所述

要更新旧示例,您必须替换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}}
              />
            )
      };
于 2019-12-12T11:16:56.807 回答
1

我在堆栈溢出中寻找答案,而答案在文档本身中。用于在 react-native 底部选项卡中使用图像作为图标。这是根据当前的 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',
        },
    }
),

您可以在此处找到更多信息。 这是带有图像的底部标签导航器

于 2020-01-15T06:07:39.543 回答
0

遇到了同样的问题,但这个解决方案对我不起作用。导航选项中定义的无效键“tabBar”... 编辑:当我从选项卡导航器中删除 tabBarOptions 时,它可以工作。而是使用 activeTintColor 和 inactiveTintColor 作为 TabBarBottom 的 props。

于 2018-01-19T05:36:16.747 回答