React Native is an amazing tool for creating beautiful and high-performing mobile applications that run on both iOS and Android. In this post, we're going to create a custom tab bar using the React Navigation library bottom tabs component.
6 October 2021
by
Aman Mittal
React Native is an amazing tool for creating beautiful and high-performing mobile applications capable of running on both iOS and Android devices. When developing these apps, it’s best to create navigation that allows users to move easily from one screen to the next. The React Navigation library does a fantastic job of providing various navigation patterns for things like stacks, tabs, and drawers that can be implemented and customized based on the UI design of your app.
To get the most out of this tutorial, you’ll want to familiarize yourself with JavaScript/ES6 and meet the following requirements in your local dev environment:
To create a new React Native app, execute the following command from a terminal window on your local dev environment. Next, navigate inside the project directory created by react-native-cli and install the following dependencies:
Please note that to successfully implement this tutorial, you will need to use the React Navigation v5 library. After installing these dependencies, import the Gesture Handler library at the top of the index.js file of your React Native app:
import 'react-native-gesture-handler';
Then, for iOS, install the CocoaPods for all these dependencies by navigating inside the ios directory in a terminal window and executing the following command. If you do not have CocoaPods installed on your local dev machine, follow the alternate command below:
cd ios && pod install
# after pods are installed
cd ..
# alternate command
npx pod-install ios
Installing react-native-vector-icons
The react-native-vector-icons module needs a bit more configuration for iOS and Android platforms.
For iOS, you need to add the following inside ios/customTabBar/Info.plist:
<key>UIAppFonts</key><array><string>AntDesign.ttf</string><string>Entypo.ttf</string><string>EvilIcons.ttf</string><string>Feather.ttf</string><string>FontAwesome.ttf</string><string>FontAwesome5_Brands.ttf</string><string>FontAwesome5_Regular.ttf</string><string>FontAwesome5_Solid.ttf</string><string>Foundation.ttf</string><string>Ionicons.ttf</string><string>MaterialIcons.ttf</string><string>MaterialCommunityIcons.ttf</string><string>SimpleLineIcons.ttf</string><string>Octicons.ttf</string><string>Zocial.ttf</string><string>Fontisto.ttf</string></array>
Then, add the following to the ios/Podfile and run a cd ios && pod update from a terminal window:
pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons'
For Android, add the following snippet in the file android/app/build.gradle:
And that’s all you need to setup the react-native-vector-icons library!
Now that we have installed and configured everything required to build and run our app, we can now edit the App.js file—otherwise known as the entry point for the React Native app.
To build the app for iOS, execute the following command npx react-native run-ios from a terminal window. Similarly, the build command for Android is npx react-native run-android.
Here is the default app running after the build for iOS:
Add mockup screens
The tab bar we’re building in this example app will display three different tabs. The first tab is used to display a list of items with images so that when the tab bar is added to the app, we can configure its translucency.
To start, let’s add them inside a separate directory called screens/ and create the first file with the name, data.js. This file will contain the mockup data that will display inside the list view in the first tab. Next, add the following code snippet:
In the above code snippet, you can see that data is an array that has different objects.
Let’s create the first tab screen and call it Home.js; this is where the array of mockup data will be used. Import the following statements inside it and then define a custom width and height for the image card. This image card is displayed inside the list view as an item. Using React Native’s Dimensions API, the width and height of the image are calculated based on the width of the device’s screen.
Using a Text component, the title of the tab is displayed, and using the ScrollView, the list of items is implemented using JavaScript’s map() method, which allows you to iterate over each item. After importing the statements, add the following snippet:
Now that our mockup data is inside one of the tab screen components, we can move on from the tab screens to our tab bar.
How to create a tab bar
Create a new directory called navigation/ at the root of the React Native project. This is where we are going to store all of our navigation configuration files. Inside it, create a new directory called TabNavigator. This new directory will store two separate files:
index.js to initiate the complete Tab Bar configuration
CustomTabBar.js to render the custom tab bar
Inside the file TabNavigator/index.js, import the createBottomTabNavigator from the @react-navigation/bottom-tabs package. Using this, a Tab object is initialized. This object allows us to define the structure of the routes using the Tab.Navigator and each route using the Tab.Screen component.
import React from 'react';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import Home from '../../screens/Home';
import Browse from '../../screens/Browse';
import Library from '../../screens/Library';
const Tab = createBottomTabNavigator();
const TabNavigator = () => {
return (
<Tab.Navigator><Tab.Screen name="Home" component={Home} /><Tab.Screen name="Browse" component={Browse} /><Tab.Screen name="Library" component={Library} /></Tab.Navigator>
);
};
export default TabNavigator;
Now, our simple tab bar configuration is done! To see it in action, wrap it with the NavigationContainer component inside the new file called navigation/RootNavigator.js. This component manages the navigation tree, and it contains the navigation state prop.
import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import TabNavigator from './TabNavigator';
const RootNavigator = () => {
return (
<NavigationContainer><TabNavigator /></NavigationContainer>
);
};
export default RootNavigator;
The last step is to import and render the Root Navigator from inside the App.js file:
import React from 'react';
import {StatusBar} from 'react-native';
import RootNavigator from './navigation/RootNavigator';
const App = () => {
return (
<><StatusBar hidden /><RootNavigator /></>
);
};
export default App;
Using an iOS simulator, we can see the tab bar we’ve created below. At this point, we haven’t added any custom styles to the tab bar, so it has a default style from the React Navigation library.
Add icons to the tab bar
To add icons to each tab, first import the Icon component from react-native-vector-icons library inside the navigation/TabNavigator/index.js file. For this example, let’s use AntDesign based icons.
// after other import statements
import Icon from 'react-native-vector-icons/AntDesign';
Using the screenOptions object on Tab.Navigator, we can enable the configuration to display icons for each tab. This object has different methods and properties that allow us to enable various configurations. One such method is called tabBarIcon, which allows us to display a custom icon for each tab. This function returns an Icon component that has props like color and size to apply tint color on the icon for each tab and define a numeric value for the size of the icon. It also has a prop called name which allows us to define which icon should be used for which screen.
If you return to the iOS simulator, you will notice that the icons for each tab route are now displayed.
Customizing the tab bar
To customize the tab bar, we’ll start by applying a tabBarOptions object. This options object has properties that allow you to set both active and inactive tint color for each of the tabs and the background color for the whole tab bar.
Next, add the following options object on Tab.Navigator:
The property elevation is set to zero in the above code snippet so that there is no shadow overlap on Android when we make the custom tab bar translucent in the next section.
Making the tab bar translucent
To make the tab bar translucent, we are going to use the BlurView component from @react-native-community/blur, and it is going to wrap a component called BottomTabBar from the @react-navigation/bottom-tabs library. This component is a React element that is used to display the actual tab bar, which is provided by a prop called tabBar on Tab.Navigator. Using this React element, the tab bar can be defined explicitly inside the CustomTabBar.js component file.
Start by adding the following snippet inside the TabNavigator/CustomTabBar.js file:
import React from 'react';
import {BottomTabBar} from '@react-navigation/bottom-tabs';
import {BlurView} from '@react-native-community/blur';
const CustomTabBar = (props) => {
return <BottomTabBar {...props} />;
};
export default CustomTabBar;
The props received by this custom React component are passed from the tabBar option. Add it on the Tab.Navigator inside the TabNavigator/index.js file:
Using the props, you can further modify the configuration of the bottom tab bar, but that’s a topic for another day and another blog post.
Inside the CustomTabBar.js file, wrap the BottomTabBar with the <BlurView> component. Note that there is a different set of props for both iOS and Android in order to implement the blur view effect.
To create a blur view effect for iOS, add blurType, which accepts the type of blur effect as a string value. On iOS, different values for light, dark, xlight, and regular are available to customize the overall appearance of the tab. And another property, blurAmount, can be applied to adjust the intensity of the blur effect.
Similarly for Android, props like overLayColor can be used to create a custom overlay, and blurRadius can be used to manually adjust the blur effect radius.
To ensure that any styles applied are made on the BlurView component and that the tab bar is displayed on top of the content for each screen, set its position to absolute.
Here is the final snippet for the CustomTabBar component:
The tab bar is now translucent! Here is an example of our app component running on an iOS simulator.
In the above example, you’ll notice that the BlurView component is set to absolute, but we still need to apply the paddingBottom property in the ScrollView component inside the Home.js tab screen.
The value of this property should be set to be the height of the whole tab bar. To get the height of the current tab bar, the @react-navigation/bottom-tabs module has a hook called useBottomTabBarHeight which provides this value.
From here, add the import statement in the screens/Home.js file and use the hook to get the height as shown below, applying it as the value of the paddingBottom style property in the ScrollView component:
Back in the iOS simulator, you will see that it works just fine now:
Running the app build on an Android simulator, the results are similar:
Conclusion
Although we only went through a single scenario for customizing the bottom tab bar in this blog post, the options for customization are endless. Familiarize yourself with the component-based configuration of the Tab Navigator in the latest version of the react-navigation library, try out a few different options, and see what works best with your branding!
If you’re looking to build a scalable cross-platform app, Crowdbotics is here to help. Our expert PMs and developers can convert your website into a native app or vice-versa or build a brand new universal app from scratch. Get in touch with us today for a detailed quote and timeline!
Platform
The Crowdbotics platform leverages AI and systematic code reuse to help you build better applications faster and with less risk.