Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color the react-navigation drawer active icon if using react-native-vector-icons

I have this react-navigation drawer:

enter image description here

I want to color the active icons green like the labels.

I'm using react-native-vector-icons for the icons.

code:

const AddMenuIcon = ({ navigate }) => (
  <View>
    <Icon
      name="plus"
      size={30}
      color="#FFF"
      onPress={() => navigate('DrawerOpen')}
    />
  </View>
)

const SearchMenuIcon = ({ navigate }) => (
  <Icon
    name="search"
    size={30}
    color="#FFF"
    onPress={() => navigate('DrawerOpen')}
  />
)

const LoginMenuIcon = ({ navigate }) => (
  <Icon
    name="user"
    size={30}
    style={{ fontWeight: '900' }}
    color="#FFF"
    onPress={() => navigate('DrawerOpen')}
  />
)

const Stack = {
  Login: {
    screen: Login,
    headerMode: 'none'
  },
  Search: {
    screen: Search,
    headerMode: 'none'
  },
  Add: {
    screen: Add,
    headerMode: 'none'
  }
}

const DrawerRoutes = {
  Login: {
    name: 'Login',
    screen: StackNavigator(Stack.Login, {
      headerMode: 'none'
    }),
    headerMode: 'none',
    navigationOptions: ({ navigation }) => ({
      drawerIcon: LoginMenuIcon(navigation)
    })
  },
  'Search Vegan': {
    name: 'Search',
    screen: StackNavigator(Stack.Search, {
      headerMode: 'none'
    }),
    headerMode: 'none',
    navigationOptions: ({ navigation }) => ({
      drawerIcon: SearchMenuIcon(navigation)
    })
  },
  'Add vegan': {
    name: 'Add',
    screen: StackNavigator(Stack.Add, {
      headerMode: 'none'
    }),
    headerMode: 'none',
    navigationOptions: ({ navigation }) => ({
      drawerIcon: AddMenuIcon(navigation)
    })
  }
}

const CustomDrawerContentComponent = props => (
  <SafeAreaView style={{ flex: 1, backgroundColor: '#3f3f3f', color: 'white' }}>
    <DrawerItems {...props} />
  </SafeAreaView>
)

const RootNavigator = StackNavigator(
  {
    Drawer: {
      name: 'Drawer',
      screen: DrawerNavigator(DrawerRoutes, {
        initialRouteName: 'Login',
        drawerPosition: 'left',
        contentComponent: CustomDrawerContentComponent,
        contentOptions: {
          activeTintColor: '#27a562',
          inactiveTintColor: 'white',
          activeBackgroundColor: '#3a3a3a',
        }
      }),
      headerMode: 'none',
      initialRouteName: 'Login'
    },
    initialRouteName: 'Login'
  },
  {
    headerMode: 'none',
    initialRouteName: 'Drawer'
  }
)

export default RootNavigator

Is there any way at all to colour the active icon the same as the active text if using react-native-vector-icons? activeTintColor doesn't work on the icon. Can we programmatically check if active? Another strange thing is rgba colours do not work on the CustomDrawerContentComponent so I can't make the background semi-transparent which is annoying. Bonus if you can help there too!

like image 478
BeniaminoBaggins Avatar asked Oct 02 '18 06:10

BeniaminoBaggins


People also ask

How do I change the drawer icon color in react-native?

How do I change the background color on my navigation drawer? Create a new Android application using application name: "Navigation Drawer" and package name: "com. Select the "Navigation Drawer Activity" as the default activity. To change the default theme color of the application, change the values in values/colors.

How do I change the background color of a drawer in react?

Using your above example, changing the background color will go thus: const Drawer = DrawerNavigator( { dOne: {screen: Screen1}, dTwo: {screen: Screen2}, }, { initialRouteName: 'dOne', contentOptions: { style: { backgroundColor: 'black', flex: 1 } }, } );


1 Answers

In react-navigation Version: 5.x

use color not tintColor providing-a-custom-drawercontent

    <Drawer.Screen name="settingscreen" component={Settings}
            options={{
                drawerLabel: "Settings",
                drawerIcon: ({ color }) => <MaterialCommunityIcons name="settings" size={24} style={[styles.icon, { color: color }]} />
            }}
        />
like image 65
SUDHIR KUMAR Avatar answered Oct 02 '22 05:10

SUDHIR KUMAR