将自定义图标添加到抽屉导航

时间:2019-01-17 12:54:47

标签: react-native navigation-drawer react-navigation

我正在尝试将自定义图标添加到我的CustomDrawerComponent中,但是什么也没发生...

App.js:

const navigationOptions = {
  headerTintColor: colors.white,
};

const drawerNavigationOption = ({ navigation }) => ({
  ...navigationOptions,
  headerLeft: (
    <TouchableOpacity onPress={() => navigation.toggleDrawer()}>
      <View>
        <Icon name="menu" size={24} color={colors.white} />
      </View>
    </TouchableOpacity>
  ),
});

const MapsStackNavigator = createStackNavigator({
  MapsNavigator: {
    screen: MapsScreen,
    navigationOptions: drawerNavigationOption,
  },
});

const AppDrawerNavigator = createDrawerNavigator(
  {
    Plans: MapsStackNavigator,
  },
  {
    contentComponent: CustomDrawerMenu,
    contentOptions: {
      inactiveTintColor: colors.doveGrey,
      activeTintColor: colors.doveGrey,
    },
  }
);

我的CustomDrawerMenu.js:

export default class CustomDrawerMenu extends Component {
  render() {
    return (
      <ScrollView
        contentContainerStyle={{
          flex: 1,
          flexDirection: "column",
          justifyContent: "space-between",
        }}
      >
        <SafeAreaView forceInset={{ top: "always", horizontal: "never" }}>
          {...}
          <DrawerItems {...this.props} />
        </SafeAreaView>
        {...}
      </ScrollView>
    );
  }
}

我的MapsScreen:

export default class MapsScreen extends React.Component {
  static navigationOptions = {
    drawerIcon: (
      <Image
        style={{ width: 24, height: 24 }}
        source={require("../../assets/icons/plan.png")}
      />
    ),
    title: "Plans",
  };

  render() {
    return (
      <Text>My map screen</Text>
    );
  }
}

但是绝对没有发生任何事情...我试图将drawerIcon添加到我的App.js > const navigationOptions中,但是也没有任何反应。

我真的不知道在哪里放置抽屉图标,因为我在文档上搜索某些YouTube视频,当我复制该内容时,它不起作用。

谢谢。

5 个答案:

答案 0 :(得分:8)

在新版本的react-navigation(5.x)

您必须做:

1-

import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import Icon from 'react-native-vector-icons/Ionicons';

2-而不是使用createDrawerNavigator,而必须使用Drawer.Navigator,如下所示:

<NavigationContainer>
    <Drawer.Navigator
        initialRouteName="Products">

        <Drawer.Screen name="Products" component={YOUR COMPONENT OR YOUR STACKNAVIGATOR} options={{
            drawerIcon: config => <Icon
                size={23}
                name={Platform.OS === 'android' ? 'md-list' : 'ios-list'}></Icon>
        }} />

        <Drawer.Screen name="Orders" component={YOUR COMPONENT OR YOUR STACKNAVIGATOR} options={{
            drawerIcon: config => <Icon
                size={23}
                name={Platform.OS === 'android' ? 'md-create' : 'ios-create'}></Icon>
        }} />

    </Drawer.Navigator>
</NavigationContainer>

答案 1 :(得分:0)

我终于找到了答案,您无法将drawerIcon添加到子屏幕的navigationOptions中。您必须这样做:

const AppDrawerNavigator = createDrawerNavigator(
  {
    Home: {
      screen: HomeStackNavigator,
      navigationOptions: {
        drawerIcon: (
          <Image
            style={{ width: 24, height: 24 }}
            source={require("./assets/icons/plan.png")}
          />
        ),
      },
    },

然后在您的HomeStack中:

const HomeStackNavigator = createStackNavigator({
  HomeNavigator: {
    screen: HomeScreen,
    navigationOptions: drawerNavigationOption,
  },
});

希望它将为某人服务!

答案 2 :(得分:0)

 <Stack.Screen name="Feed" component={Feed} options={{ title: 'Feed',
            drawerIcon: ({ focused, size }) => (
                <Image
                  source={require('../../../assets/icons/icon-email.png')}
                  style={[{ height: 20, width: 20 }]}
                /> )       
            }} />

答案 3 :(得分:0)

const AppDrawerNavigator = createDrawerNavigator(
 {
     Home: {
       screen: HomeStackNavigator,
       navigationOptions: {
         drawerIcon: (
          <View>
           <Image
             style={{ width: 24, height: 24 }}
             source={require("./assets/icons/plan.png")}
           />
          </View>
         ),
       },
     },

添加之前,您将获得原始图像样式

答案 4 :(得分:0)

希望这可以节省一些人的时间..

  import { NavigationContainer } from "@react-navigation/native";
   <NavigationContainer>
      <Drawer /> //import from your folder/file
    </NavigationContainer>

抽屉文件

   import { createDrawerNavigator } from "@react-navigation/drawer";
   import DrawerContain from "./DrawerContain";
   import StackNavigatore from "./stackNavigatore";
    import ProductHome from "../product/ProductHome";
   import Contact from "./ContactUs";
   import About from "./About";
      import HomeOrder from "./orderStack";
   function DrawerNavigator() {
   return (
   <Drawer.Navigator
    drawerContent={(props) => <DrawerContain {...props} />}
    drawerContentOptions={
    {
      // activeTintColor: "#e91e63",
      // itemStyle: { marginVertical: 5 },
    }
  }
>
  <Drawer.Screen name="Home" component={StackNavigatore} />
  <Drawer.Screen
    name="Order"
    component={HomeOrder}
  />
  <Drawer.Screen name="Contact Us" component={Contact} />
  <Drawer.Screen name="About Us" component={About} show={false} />
</Drawer.Navigator>
  );
}

  export default DrawerNavigator;

抽屉容器文件

  import {
   DrawerContentScrollView,
  DrawerItemList,
  DrawerItem,
 } from "@react-navigation/drawer";
  import { View, StyleSheet } from "react-native";
  import { useNavigation } from "@react-navigation/native";
 import React from "react";
 import Ionicons from "react-native-vector-icons/Ionicons";
 import { Drawer, Text } from "react-native-paper";

    function DrawerContain({ ...props }) {
    //   const navigation = useNavigation();
    const image = require("../../assets/img/rupee.png");
   return (
<>
    <Drawer.Section>
      <DrawerItem
        icon={({ color, size }) => (
          <Ionicons name="home-outline" color={color} size={size} /> <<--- with 
        vectore icon
        )}
        label="Sell prodcuts to customer"
        // onPress={() => props.navigation.navigate('route to screen')}
      />
      <Drawer.Item
        icon={image}   <<---- from local storage
        label="Orders"
        onPress={() => props.navigation.navigate("Order")}
      />
</> 
  )
 }