首页 > 解决方案 > 无法将图像添加到标签栏项目反应导航

问题描述

# 我正在尝试将图像添加到标签栏项目,但无法在反应导航中加载 #

## 我指的是https://github.com/react-navigation/react-navigation/issues/1205#issuecomment-296708338 ##

import React from 'react';
import { Text, View, Image } from 'react-native';
import { createBottomTabNavigator, createAppContainer } from 'react-navigation'
import ScreenOne from './ScreenOne';
import Screentwo from './Screentwo';
import Preferences from './Preferences';




const TabNavigator = createBottomTabNavigator(
  {
  Home: {
    screen : ScreenOne,
    navigationOptions: {
      showLabel: false,
      tabBarIcon: <Image style={{ width: 30, height: 30 }} source={require('../images/Help_Header_Icon.png'
      )}/>,
      showIcon: true,
      activeTintColor: '#00000',
      inactiveTintColor: '#000000'
    }
  },
  Settings: Screentwo,
  Preference: Preferences
},
{  
  initialRouteName: "Home"
}
);


export default createAppContainer(TabNavigator);

### 期望在标签栏项目中显示图像并隐藏标签栏标签###

标签: iosreact-nativereact-navigation

解决方案


标签栏图标

React Element 或给定的函数{ focused: boolean, horizontal: boolean, tintColor: string }返回一个 React.Node,以显示在选项卡栏中。horizontaltrue当设备处于横向和false纵向时。每当设备方向发生变化时,都会重新呈现图标。

用法

 {
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, horizontal, tintColor, image }) => {
        const { routeName } = navigation.state;
        let imagepath;
        if (routeName === "Home") {
          imagepath = require('../images/Help_Header_Icon.png');
        } else if (routeName === "Settings") {
          imagepath = require('../images/Settings.png');
        } else if (routeName === "Preference") {
          imagepath = require('../images/Preference.png');
        }
        return (
          <Image
            style={{ width: 30, height: 30, resizeMode: "stretch" }}
            source={imagepath}
          />
        );

推荐阅读