Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom icon to drawer navigation

I am trying to add custom icon to my CustomDrawerComponent, but nothing happen...

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,
    },
  }
);

My 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>
    );
  }
}

My 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>
    );
  }
}

But absolutely nothing happened... I tried to add drawerIcon to my App.js > const navigationOptions but nothing happened aswell.

I do not really know where to place drawerIconm because I search on the doc, on some YouTubes video and when I reproduced the same, it does not work.

Thank you.

like image 997
GuillaumeRZ Avatar asked Jan 17 '19 12:01

GuillaumeRZ


People also ask

How do I add an icon in custom drawer in react native?

You have to do like so : const AppDrawerNavigator = createDrawerNavigator( { Home: { screen: HomeStackNavigator, navigationOptions: { drawerIcon: ( <Image style={{ width: 24, height: 24 }} source={require("./assets/icons/plan.

How do you add pictures to drawer Navigator?

Navigator drawerContent={(props) => { return ( <SafeAreaView style={{ flex: 1 }}> <View style={{ height: 100, alignItems: "center", justifyContent: "center", }} > <Image source={require("../assets/logo. png")} width={50} height={50} /> </View> <DrawerItemList {...

How do you add an image to the navigation drawer in react native?

Options to Add Image Icon Inside Navigation Bar To set any Component/Image/Button for all the screen you can use screenOptions while creating Navigator Stack. Stack. Navigator is the parent component of all the Stack. Screen so once you set component here it will be reflected in all the screens under it.


2 Answers

In the new version of react-navigation(5.x)

You have to do :

1-

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

2- Instead of using createDrawerNavigator you have to use Drawer.Navigator as below :

<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>
like image 190
AbolfazlR Avatar answered Nov 23 '22 18:11

AbolfazlR


I finally found the answer myself, you can not add drawerIcon to navigationOptions of the child-screen. You have to do like so :

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

And then in your HomeStack :

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

Hope it'll serve to someone !

like image 43
GuillaumeRZ Avatar answered Nov 23 '22 18:11

GuillaumeRZ