17

我正在尝试为我的 react-navigation 抽屉中的每个屏幕添加一个图标,但该图标没有出现。

这是我的代码:

function Drawer() {
  return (
      <Drawer.Navigator 
       drawerStyle={styles.drawer}
        initialRouteName="Home" 
        drawerPosition='right'
        drawerContentOptions={{
        activeTintColor: 'white',
        inactiveTintColor: 'white',
        itemStyle: { alignItems:'flex-end' },
       }}>
        <Drawer.Screen name="AppTab" component={AppTab1} options={{ headerStyleInterpolator: forFade ,title:"home" ,icon:<Image source={require('./images/icons/plumbing-b.png')} style={styles.drawerActive}/> }} />
        <Drawer.Screen name="News" component={NotificationsScreen} options={{ headerStyleInterpolator: forFade ,title:"new items" icon:<Image source={require('./images/icons/plumbing-b.png')} style={styles.drawerActive}/> }} />
        
      </Drawer.Navigator>
    
  );
}


export function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          options={{
            headerTitleAlign:"center",
            headerRight: ({}) => <HeaderRight />,
            headerLeft: ({}) => <Search />
          }}
          component={Drawer}
          name="Drawer"
        />
        <Stack.Screen name="Product" component={Product} options={{title:"product"}} />
        {/*
         * Rest Screens
         */}
      </Stack.Navigator>
    </NavigationContainer>
  );
}

在文档中,仅在 DrawerItem 中提到了添加图标:

https://reactnavigation.org/docs/en/drawer-navigator.html

4

4 回答 4

27

您只需在选项中添加抽屉图标

<Drawer.Navigator>
    <Drawer.Screen name="Home" component={Home}
        options={{
           title: 'Home',
           drawerIcon: ({focused, size}) => (
              <Ionicons
                 name="md-home"
                 size={size}
                 color={focused ? '#7cc' : '#ccc'}
              />
           ),
        }}
   />
</Drawer.Navigator>

你也可以像这样直接传递颜色

...
drawerIcon: ({color, size}) => (
            <Ionicons
               name="md-home" size={size} color={color}
            />
          ),
...

这里我使用了 Ionicons 作为图标,您可以使用自己的图标组件或从 'react-native-vector-icons/Ionicons' 导入 Ionicons。

于 2020-05-19T19:14:03.853 回答
11

传入drawerIcon你的屏幕options

options={{
  title: 'Product',
  drawerIcon: ({ focused, size }) => (
    <Image
      source={require('./images/icons/plumbing-b.png')}
      style={[focused ? styles.drawerActive : styles.drawerInActive, { height: size, width: size }]}
    />
}}

https://reactnavigation.org/docs/en/drawer-navigator.html#drawericon

于 2020-02-25T00:26:52.383 回答
1

您可以在 DrawerContent 组件中为 DrawerItem 添加图标。

在应用程序中:

function Drawer() {
  return (
      <Drawer.Navigator 
       drawerStyle={styles.drawer}
        initialRouteName="Home" 
        drawerPosition='right'
        drawerContentOptions={{
          activeTintColor: 'white',
          inactiveTintColor: 'white',
          itemStyle: { alignItems:'flex-end' },
        }}
        drawerContent={props => <DrawerContent {...props}/>}
      >
        <Drawer.Screen name="AppTab" component={AppTab1} options={{ headerStyleInterpolator: forFade ,title:"home" ,style={styles.drawerActive}/> }} />
        <Drawer.Screen name="News" component={NotificationsScreen} options={{ headerStyleInterpolator: forFade ,title:"new items" style={styles.drawerActive}/> }} />

      </Drawer.Navigator>

  );
}

在抽屉内容中:

                  <DrawerItem
                    label='News'
                    onPress={() => 
                      props.navigation.navigate('News')}                                           
                    icon={() =>
                      <Image
                        style={styles.icon}
                        source={require('./images/icons/plumbing-b.png')
                      />
                    }
                  />
于 2021-02-25T09:02:58.323 回答
-1

我使用抽屉内容配置抽屉内容:步骤如下... 1.使用屏幕函数 DrawerStack({ route, navigation }) { return (

  drawerContent={(props) => <DrawerContent {...props} />}

  drawerStyle={{
    backgroundColor: "green",
    alignItems: "center",

    paddingTop: 100
  }}
>
  {/* //it is must to define the screens here */}
  <Drawer.Screen name="Drawer1" component={Drawer1}
  />
  <Drawer.Screen name="Drawer2" component={Drawer2} />
</Drawer.Navigator>

) }

2.使用drawerContent自定义抽屉内容:

功能抽屉堆栈({路线,导航}){返回(

  drawerContent={(props) => <DrawerContent {...props} />}

  drawerStyle={{
    backgroundColor: "green",
    alignItems: "center",

    paddingTop: 100
  }}
>
  {/* //it is must to define the screens here */}
  <Drawer.Screen name="Drawer1" component={Drawer1}
  />
  <Drawer.Screen name="Drawer2" component={Drawer2} />
</Drawer.Navigator>

) }

于 2020-02-25T08:22:46.940 回答