我有这个反应导航抽屉:
我想将活动图标涂成绿色,如标签一样。
我正在使用react-native-vector-icons作为图标。
代码:
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
如果使用react-native-vector-icons,有什么办法可以为活动图标和活动文本涂上相同的颜色? activeTintColor
在该图标上不起作用。我们可以通过编程方式检查是否处于活动状态吗?另一个奇怪的事情是,rgba颜色在CustomDrawerContentComponent
上不起作用,因此我不能使背景变得半透明,这很烦人。如果您也可以在那里获得奖金!
答案 0 :(得分:4)
在react-navigation版本中:5.x
使用颜色而不是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 }]} />
}}
/>
答案 1 :(得分:2)
对于5.x版,您必须像以下示例一样使用activeTintColor
。
function App() {
return (
<Drawer.Navigator
drawerContentOptions={{
activeTintColor: 'blue',
itemStyle: { marginVertical: 8, marginHorizontal: 8 },
}}
>
<Drawer.Screen
name="Home"
component={AppNavigator}
options={{
drawerIcon: ({ color }) => (
<AntDesign name={"calendar"} size={20} color={color} />
),
}}
/>
<Drawer.Screen
name="Completed"
component={CompletedContainer}
hideStatusBar
options={{
drawerIcon: ({ color }) => (
<Entypo name={"list"} size={20} color={color} />
),
}}
/>
<Drawer.Screen
name="Settings"
component={SettingsContainer}
hideStatusBar
options={{
drawerIcon: ({ color }) => (
<Feather name={"settings"} size={20} color={color} />
),
}}
/>
</Drawer.Navigator>
);
}
答案 2 :(得分:1)
您可以将tintColor
属性动态传递到您的图标组件,该图标组件会自动解析为有效或无效的色彩:
drawerIcon: ({tintColor}) => <MaterialIcons name="home" size={20} color={tintColor} />,
此外,默认情况下,不活动的图标容器似乎也具有一定的透明度,因此您可能需要将opacity
设置为1:
<DrawerItems iconContainerStyle={{opacity: 1}} ... />
答案 3 :(得分:0)
对我来说,它起作用了
<Drawer.Screen name="settingscreen" component={Settings}
options={{
drawerLabel: "Settings",
drawerIcon:(({color}) => <AntDesign name="home" size={30} color={color} />) />
}}
/>