How to get a background image in createBottomTabNavigator? - react-native

I have been trying to get a background image for the tabbar. I tried using tabBarComponent but it hides the tabs under it.
The code that I am using is
export default MainNavigation = createBottomTabNavigator(
{
Profile:{
screen: Profile,
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
return <Image source={require('./images/tab_explore.png')} />
}
}),
}
},
{
tabBarComponent: props =>{
return(
<View style={{backgroundColor:"black"}}>
<Image
style={{ width:'100%', height: 80 }}
source={ require('./images/bottom_btn.png')} />
</View>
);
}
})
Does anyone know how to solve the problem? Thanks in advance!
Current Output:
It should show the tabs on top of orange color.

Maybe it is too late, but I wish this answer will help many people. In React Navigation version 2 you can do it like this:
import { createBottomTabNavigator, BottomTabBar } from 'react-navigation-tabs';
You don't need to install react-navigation-tabs if you have installed 'react-navigation'.
Then create TabBarComponent by const TabBarComponent = (props) => (<BottomTabBar {...props} />);. You will use tabBarComponent key in createBottomTabNavigator. BackgroundColor: 'transparent' for default tabs and VOILA!
The following code will give you the main idea
export const Tabs = createBottomTabNavigator({
Posters: {
screen: Posters,
navigationOptions: {
tabBarLabel: 'AFİŞA',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="local-movies" size={24} color={tintColor} />
)
}
},
ComingSoon: {
screen: ComingSoon,
navigationOptions: {
tabBarLabel: 'TEZLİKLƏ',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="movie" size={24} color={tintColor} />
)
}
},
Tickets: {
screen: Tickets,
navigationOptions: {
tabBarLabel: 'BİLETLƏR',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="confirmation-number" size={24} color={tintColor} />
),
}
},
More: {
screen: More,
navigationOptions: {
tabBarLabel: 'MENU',
tabBarIcon: ({tintColor}) => (
<MaterialIcons name="more-horiz" size={24} color={tintColor} />
),
tabBarOnPress: ({ navigation }) => {
return <Text>sd</Text>
}
}
},
},
{
order: ['Posters', 'ComingSoon', 'Tickets', 'More' ],
tabBarOptions: {
activeTintColor: colors.darkYellow(),
inactiveTintColor: 'white',
labelStyle: {
fontSize: 12,
fontWeight: 'bold',
fontFamily: defaults.fontFamily
},
style: styles.tabBar,
},
tabBarComponent: props =>{
return(
<React.Fragment>
<TabBarComponent {...props} />
<Image style={styles.fakeTabBackground}/>
</React.Fragment>
)
}
})

I wish I could comment but I don't have enough reputation to do that.
<React.Fragment>
<ImageBackground
source={require('./images/bottom_btn.png')}
style={{width: '100%', height: 80}}
>
<TabBarComponent {...props} />
</ImageBackground>
</React.Fragment>
This worked for me. Spent almost a day figuring out the <ImageBackground> thing.

With the latest version of the 'react-navigation-tabs' the above solutions don't work, what we need to do is, we should pass the following three properties to the BottomTabBar component explicitly:
<BottomTabBar
{...this.props}
getButtonComponent={this.getButtonComponent}
getAccessibilityRole={() => 'button'}
getAccessibilityStates={({focused}) => (focused ? ['selected'] : [])}
/>
So the full code for this example is as follows :
class TabBar extends React.Component {
render() {
return (
<BottomTabBar
{...this.props}
getButtonComponent={this.getButtonComponent}
getAccessibilityRole={() => 'button'}
getAccessibilityStates={({focused}) => (focused ? ['selected'] : [])}
/>
);
}
getButtonComponent({route}) {
if (route.key === 'Other') {
return () => <View />; // a view that doesn't render its children
} else {
return null; // use the default nav button component
}
}
}
const Tabs = createMaterialTopTabNavigator(
{
home: {
screen: Home_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'home',
tabBarIcon: ({tintColor}) => (
<FontAwesome name="home" size={23} color={tintColor} />
),
}),
},
favourites: {
screen: Favourites_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'favourites',
tabBarIcon: ({tintColor}) => (
<Ionicons name="md-star" size={25} color={tintColor} />
),
}),
},
calendar: {
screen: Calendar_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'calendar',
tabBarIcon: ({tintColor}) => (
<Feather name="calendar" size={23} color={tintColor} />
),
}),
},
shoppingList: {
screen: ShoppingList_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'shopping List',
tabBarIcon: ({tintColor}) => (
<Feather name="shopping-bag" size={23} color={tintColor} />
),
}),
},
profile: {
screen: Profile_StackNavigator,
navigationOptions: ({navigation}) => ({
title: 'profile',
tabBarIcon: ({tintColor}) => (
<Feather name="user" size={23} color={tintColor} />
),
}),
},
},
{
tabBarPosition: 'bottom',
swipeEnabled: false,
animationEnabled: true,
tabBarOptions: {
showIcon: true,
activeTintColor: Colors.DARK.ICON_ACTIVE,
inactiveTintColor: Colors.DARK.ICON_INACTIVE,
style: {
backgroundColor: 'transparent',
},
labelStyle: {
textAlign: 'center',
fontSize: 10,
textTransform: 'capitalize',
color: Colors.DARK.ICON_INACTIVE,
marginBottom: 1,
width: 70,
},
indicatorStyle: {
borderBottomColor: Colors.DARK.ICON_INACTIVE,
borderBottomWidth: 2,
},
},
tabBarComponent: (props) => {
return (
<React.Fragment>
<ImageBackground
source={require('../../assets/images/image_bottom.jpg')}
style={{width: '100%', height: 60}}>
<TabBar {...props} />
</ImageBackground>
</React.Fragment>
);
},
},
);

Related

React Native - making a second tab bar navigator for a modal screen

Building out a relatively simple app on React Native, I currently have a tabBarNavigator where one of the icons on the tab bar opens a pop-up modal. I'm then trying to create another, separate, tab bar at the bottom of the modal.
However although I have created a tabBarNavigator and appContainer in my root App.js and have exported this container (passing it through my modal component), the modal then loads the base stack rather than the modal stack which I created - obviously not desired behavior!
The relevant portion of App.js:
const ModalNavigator = createBottomTabNavigator({
Photo: { screen: PrivacySettings,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <Feather name="camera" size={24} color="black" />
}
},
Camera: { screen: HelpSettings,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <Feather name="type" size={24} color="black" />
}
},
Text: { screen: ContactUs,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <Feather name="type" size={24} color="black" />
}
}
});
export const ModalContainer = createAppContainer(ModalNavigator);
const AppContainer = createStackNavigator({
default: createBottomTabNavigator({
Home: { screen: Home,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faHome} color={tintColor}/>
}
},
Search: { screen: Contacts,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faSearch} color={tintColor}/>
}
},
AddPhoto: { screen: () => null,
navigationOptions: {
headerTitle: "Upload Photo",
tabBarIcon: <AddPhotoButton/>,
headerMode: 'none',
}
},
Likes: {screen: Vault,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faHeart} color={tintColor}/>
}
},
Settings: {screen: Me,
navigationOptions: {
headerMode: false,
tabBarIcon: ({ tintColor }) => <FontAwesomeIcon size={30} icon={faUser} color={tintColor}/>
}
}
},
{
defaultNavigationOptions: {
tabBarOnPress: ({ navigation, defaultHandler }) => {
if (navigation.state.key === 'AddPhoto') {
navigation.navigate('addPhotoModal')
} else {
defaultHandler()
}
},
cardStyle: {
backgroundColor: "transparent",
opacity: 1
}
},
tabBarOptions: {
showLabel: false,
activeTintColor: '#1A86CB',
inactiveTintColor: 'black'
},
initialRouteName: "Home",
}),
addPhotoModal: {
screen: AddPhotoModal }
}, {
mode: 'modal',
headerMode: 'none',
transparentCard: true,
}
)
const Routes = createStackNavigator({
Home: { screen: AppContainer,
navigationOptions: {
headerShown: false }
},
SignIn: { screen: SignIn },
AddContact: { screen: AddContact,
navigationOptions: {
headerTitle: "Add Contact" }
},
ContactDetails: {screen: ContactDetails },
PrivacySettings: {screen: PrivacySettings,
navigationOptions: {
headerTitle: "Privacy",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
NotificationSettings: {screen: NotificationSettings,
navigationOptions: {
headerTitle: "Notifications",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
HelpSettings: {screen: HelpSettings,
navigationOptions: {
headerTitle: "Help",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
InviteSettings: {screen: InviteSettings,
navigationOptions: {
headerTitle: "Invite Friends",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
ContactUs: {screen: ContactUs,
navigationOptions: {
headerTitle: "Contact Us",
headerStyle: {
backgroundColor: 'white',
shadowColor: 'transparent'
}}
},
},
{ initialRouteName: "Home",
});
const AuthStack = createStackNavigator({
Login: SignIn,
})
export default createAppContainer(
createSwitchNavigator(
{
Loading: LoadingScreen,
App: Routes,
Auth: AuthStack
},
{
initalRouteName: LoadingScreen
}
)
)
The relevant portion of the Modal Component:
import ModalContainer from '../../App'
export default class AddPhotoModal extends React.Component {
render() {
return(
<View style={{backgroundColor:"#000000CC", flex:1}}>
<View style={{ backgroundColor:"#ffffff", marginLeft: 0, marginRight: 0, marginTop: 240, padding: 20, borderRadius: 20, flex: 1, }}>
<View style={styles.header}>
<TouchableOpacity style={{position: 'absolute'}} onPress={() => this.props.navigation.goBack()}>
<Text style={styles.buttonFont}>Back</Text>
</TouchableOpacity>
<TouchableOpacity style={{position: 'absolute', right: 0}} onPress={() => this.props.navigation.navigate('UploadScreen')}>
<Text style={styles.buttonFont}>Continue</Text>
</TouchableOpacity>
</View>
<ModalContainer/>
</View>
</View>
);
}
}
Edit:
With the below answer, I'm now getting this. Is there a way to place the tabbar as a child of the modal component?
https://ibb.co/kcTq0X3
'ModalContainer' is not a part of the 'Routes', So it is not known to the SwitchNavigator.
Add ModalContainer as a part of 'Routes' stack.
And to show the ModalContainer bottomTabs, navigate to ModalContainer on click of the tabIcon

React Drawer Navigation menu icon is not showing

Please note that this is my route configuration with the "Stack, Drawer and Tab Navigation"
Stack and the tab navigation is working fine. But the drawer icon is not showing at all and the drawer is working only If I slide the window manually. How to show the drawer icon and use it to open the drawer window? Please help me to sort this out.
import React from "react"
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createBottomTabNavigator } from 'react-navigation-tabs'
import { createDrawerNavigator } from 'react-navigation-drawer'
import Icon from 'react-native-vector-icons/Feather'
import HomeScreen from './screens/home/HomeScreen'
import ControlScreen from './screens/controls/ControlScreen'
import MoreScreen from './screens/more/MoreScreen'
import SignUpScreen from './screens/signup/SignUpScreen'
import AboutScreen from './screens/about/AboutScreen'
const TabContainer = createBottomTabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="home" size={20} color={`${focused ? tintColor : '#404040'}`} />,
}
},
Controls: {
screen: ControlScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="sliders" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
More: {
screen: MoreScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="more-horizontal" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
},{
tabBarposition: 'bottom',
swipeEnabled: false,
tabBarOptions: {
activeTintColor: '#069',
inactiveTintColor: '#404040',
labelStyle: {
fontSize: 14,
paddingTop: 0,
paddingBottom: 0
},
style:{
borderTopWidth: 1,
borderTopColor:'#333333',
position: 'absolute',
bottom: 0,
left: 0
},
animationEnabled: false,
tabStyle: {
paddingBottom: 15,
paddingTop: 0,
height: 75,
marginBottom: 0,
},
}
}
)
const StackContainer = createStackNavigator({
TabContainer,
},{
defaultNavigationOptions: {
headerShown: false
}
})
const MainDrawerNavigator = createDrawerNavigator({
Home: {
screen: StackContainer,
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
title: 'Home',
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="home" size={20} color={'#404040'} />
),
}
},
SignUp: {
screen: SignUpScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="user" size={20} color={'#404040'} />
),
}
},
About: {
screen: AboutScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="alert-circle" size={20} color={'#404040'} />
),
}
},
},
{
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
navigationOptions: {
drawerLockMode: 'locked-closed',
},
drawerPosition: 'left',
headerStyle: { backgroundColor: '#E73536' },
headerTintColor: 'white',
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle',
},
)
export default createAppContainer(MainDrawerNavigator)
You need to include your drawerNavigator inside stackNavigator and you can provide the default navigation options there.
I had edited your code check this:
import React from "react"
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createBottomTabNavigator } from 'react-navigation-tabs'
import { createDrawerNavigator } from 'react-navigation-drawer';
import Icon from 'react-native-vector-icons/Feather'
import HomeScreen from './screens/Example'
import ControlScreen from './screens/Main'
import MoreScreen from './screens/Sample'
import SignUpScreen from './screens/login'
import AboutScreen from './screens/login'
const TabContainer = createBottomTabNavigator({
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="home" size={20} color={`${focused ? tintColor : '#404040'}`} />,
}
},
Controls: {
screen: ControlScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="sliders" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
More: {
screen: MoreScreen,
navigationOptions: {
tabBarIcon: ({ tintColor, focused }) => <Icon name="more-horizontal" size={20} color={`${focused ? tintColor : '#404040'}`} />
}
},
},{
tabBarposition: 'bottom',
swipeEnabled: false,
tabBarOptions: {
activeTintColor: '#069',
inactiveTintColor: '#404040',
labelStyle: {
fontSize: 14,
paddingTop: 0,
paddingBottom: 0
},
style:{
borderTopWidth: 1,
borderTopColor:'#333333',
position: 'absolute',
bottom: 0,
left: 0
},
animationEnabled: false,
tabStyle: {
paddingBottom: 15,
paddingTop: 0,
height: 75,
marginBottom: 0,
},
}
}
)
const MainDrawerNavigator = createDrawerNavigator({
Home: {
screen: TabContainer,
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
title: 'Home',
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="home" size={20} color={'#404040'} />
),
}
},
SignUp: {
screen: SignUpScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="user" size={20} color={'#404040'} />
),
}
},
About: {
screen: AboutScreen,
navigationOptions: {
drawerIcon: ({ tintColor }) => (
<Icon name="alert-circle" size={20} color={'#404040'} />
),
}
},
},{
initialRouteName:"Home"
}
)
const StackContainer = createStackNavigator({
MainDrawerNavigator,
}, {
defaultNavigationOptions: ({ navigation }) => {
return {
headerTitle:"Welcome!",
headerLeft: (
<Icon
style={{ paddingLeft: 10 }}
onPress={() => navigation.openDrawer()}
name="menu"
size={30}
/>
)
};
}
})
export default createAppContainer(StackContainer)
Hope this helps!

TabIcon count not updated if record is deleted on tab focus in react-native CLI

Regarding my question from SO: Mapping view not updating on tab navigator in react-native, I have added function to delete the product from cart. And yes the cart view is reducing after deleting, but the tabicon badge count is still on last count, until I press on any tab other than cart.
For adding product cart to object, yes it will update the count accordingly. Just when delete record from object, the count is not updated.
My script on cart screen has follow suggestion from linked posting:-
componentDidUpdate(prevProps) {
if (prevProps.isFocused !== this.props.isFocused) {
this.setState({ prodData: appGlobal.ObjProduct });
}
}
shouldComponentUpdate(nextProps, nextState) {
if (this.props.prodData !== nextState.prodData) {
return true;
}
return false;
}
On TabHelpers class:-
const tabScreen = createBottomTabNavigator(
{
'Home': {
screen: homeStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name='home' size={20} color={tintColor} />
),
},
},
'Favourite': {
screen: favStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name='heart' size={20} color={tintColor} />
),
},
},
'Cart': {
screen: cartStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<View>
<Icon name='shopping-cart' size={20} color={tintColor} />
{typeof appGlobal.ObjProduct !== 'undefined' && appGlobal.ObjProduct.length > 0 ?
<Badge style={{ position: 'absolute', right: 10, top: 0, height: 13 }}>
<Text style={{ color: 'white', fontSize: 8 }}>{appGlobal.ObjProduct.length}</Text>
</Badge>
: null}
</View>
),
},
},
'Profile': {
screen: profileStack,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name='user' size={20} color={tintColor} />
),
},
}
},
{
initialRouteName: 'Home',
headerMode: 'none',
lazy: false,
backBehavior: 'initialRoute',
swipeEnabled: true,
animationEnabled: true,
tabBarPosition: 'bottom',
tabBarOptions: {
showLabel: true,
showIcon: true,
activeTintColor: '#ffffff',
activeBackgroundColor: '#4961c4',
inactiveTintColor: '#4962a6',
style: {
backgroundColor: '#475691',
},
labelStyle: {
textAlign: 'center',
fontWeight: 'bold',
},
indicatorStyle: {
borderBottomColor: '#FFFFFF',
borderBottomWidth: 2,
},
},
defaultNavigationOptions: ({ navigation }) => ({
tabBarOnPress: ({ navigation, defaultHandler }) => {
navigation.popToTop();
defaultHandler();
},
}),
}
);
export default createAppContainer(tabScreen);
Did I miss something or wrongly apply?

Passing this.state into static function

I am trying to set the headerTitle in the header bar to be the username of the logged in user: this.state.username. However, when I try to set navigationOptions to a function, I am unable to pass in the current state.
What would be the best way to set the headerTitle inside the static navigationOptions? I believe that navigationOptions must remain static, otherwise nothing will be displayed.
class profile extends React.Component {
static navigationOptions =
{
headerTitle: this.state.username
};
const TabStack = createBottomTabNavigator(
{
Home: {
screen: home,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="home" size={25} style={{ color: tintColor }} />
),
},
},
Explore: {
screen: explore,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="search" size={25} style={{ color: tintColor }} />
),
}
},
Profile: {
screen: profile,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="person" size={25} style={{ color: tintColor }} />
),
}
},
Create: {
screen: createCompetition,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="add" size={25} style={{ color: tintColor }} />
),
}
},
Payment: {
screen: payment,
navigationOptions: {
tabBarIcon: ({ tintColor }) => (
<Icon name="attach-money" size={25} style={{ color: tintColor }} />
),
title: 'Payment',
}
}
},
{
tabBarOptions: {
showIcon: true,
showLabel: false,
activeTintColor: 'black',
style: { backgroundColor: 'white', }
},
},
)
TabStack.navigationOptions = ({ navigation, screenProps }) => {
const childOptions = getActiveChildNavigationOptions(navigation, screenProps)
return {
title: childOptions.title,
headerLeft: childOptions.headerLeft,
headerRight: childOptions.headerRight,
}
}
So instead of using react navigation Header ive disabled it and used my own cutsom,
This is my Header.js :
import React, {Fragment, Component} from 'react';
import {View, Text, TouchableOpacity, Image, SafeAreaView} from 'react-native';
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from 'react-native-responsive-screen';
import {headerStyles} from '../style/headerStyles';
export default class Header extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<View
style={{
flexDirection: 'row',
paddingBottom: hp('3.125%'),
justifyContent: 'space-between',
alignItems: 'center',
}}>
<View style={{opacity: 0}}>
<Image source={require('../assets/images/crossIcon.png')} />
</View>
<View style={{flexShrink: 1}}>
<Text style={headerStyles.headerText}>{this.props.title}</Text>
</View>
<View style={{left: -20, paddingLeft: 10, marginLeft: 10}}>
<TouchableOpacity
style={headerStyles.imageView}
onPress={() => this.props.navigation.goBack()}>
<Image
style={{height: 15, width: 15}}
source={require('../assets/images/crossIcon.png')}
/>
</TouchableOpacity>
</View>
</View>
);
}
}
and you can import in any component like :
import Header from '../components/Header';
return(
<Header title={this.state.userName} navigation={this.props.navigation} />
)
With this you have enough power to do anything you want with header.
Hope it helps. feel free for doubts.
I assume you are using react-navigation, I suggest you set a navigation param in your component and then get the param in the navigationOptions.
after login do this:
navigation.setParams({ headerTitle: this.state.userName})
and then inside your navigationOptions where you already have navigation:
return { title: navigation.getParam('headerTitle') }

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

I have this react-navigation drawer:
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!
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 }]} />
}}
/>
You can dynamically pass the tintColor prop to your icon component which automatically resolves to the active or inactive tint color:
drawerIcon: ({tintColor}) => <MaterialIcons name="home" size={20} color={tintColor} />,
Also, inactive icon containers seem to come with some transparency by default so you might want to set the opacity to 1:
<DrawerItems iconContainerStyle={{opacity: 1}} ... />
For version 5.x, you have to use activeTintColor like following sample.
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>
);
}
For me it worked:
<Drawer.Screen name="settingscreen" component={Settings}
options={{
drawerLabel: "Settings",
drawerIcon:(({color}) => <AntDesign name="home" size={30} color={color} />) />
}}
/>
In the current version 6.x you will have to declare this way:
<Drawer.Screen name="Home" component={Home}
options={{
drawerActiveTintColor: 'red',
drawerIcon:(tintColor) =>(
<Icon name='home' color={tintColor.color} />
//console.log(tintColor)
)
}}
/>
and if you console log tintColor you'll get:
Object{
"color": "red",
"focused": true,
"size": 24,
}