Skip to main content

React Native Top Tab Navigator (createMaterialTopTabNavigator)

The material style createMaterialTopTabNavigator is used to create tab navigator on the top of the screen. It provides functionality to create and display multiple screens routers. These screens are switches between each other by tapping route or swiping horizontally. The tab screen components are mounted when they are focused.
The createMaterialTopTabNavigator function of react-navigation library facilitates us to implement top tab navigator.

  1. createMaterialTopTabNavigator(RouteConfigs, TabNavigatorConfig);  

React Native Top Tab Navigator Example

Let's create a top tab navigator with custom status bar and header section. In this example, we will create three different screens for "Home", "Profile" and "Settings" router. Each router screens are created in separate files.


The directory structure of the application
Create a src directory in your route project. Inside the src directory create index.js file and two other directories lib and screens. In the screens directory, we place three screens file index.js (HomeScreen)profile.js (ProfileScreen), and settings.js (SettingsScreen). In the lib directory, we implement createMaterialTopTabNavigator to create top tab navigator.

React Native Top Tab Navigator

topNavigation/index.js

Make the few changes in the topNavigation/index.js file (replace './App' with './src').

  1. import {AppRegistry} from 'react-native';  
  2. import App from './src';  
  3. import {name as appName} from './app.json';  
  4.   
  5. AppRegistry.registerComponent(appName, () => App);  
Create the classes and import Icon from 'react-native-vector-icons/Ionicons' package. Implement tabBarIcon and add Icon tag in it.
src/screens/index.js

  1. import React, {Component} from 'react';  
  2. import {View,Text} from 'react-native';  
  3. import Icon from 'react-native-vector-icons/Ionicons';  
  4. export default class HomeScreen extends Component{  
  5.     render() {  
  6.         return(  
  7.             <View>  
  8.                 <Text>This is Home Screen</Text>  
  9.             </View>  
  10.         )  
  11.     }  
  12. }  
  13. HomeScreen.navigationOptions={  
  14.             tabBarIcon:({tintColor, focused})=>(  
  15.             <Icon  
  16.                 name={focused ? 'ios-home' : 'md-home'}  
  17.                 color={tintColor}  
  18.                 size={25}  
  19.             />  
  20.         )  
  21. }  
src/screens/profile.js

  1. import React, {Component} from 'react';  
  2. import {View,Text} from 'react-native';  
  3. import Icon from 'react-native-vector-icons/Ionicons';  
  4. export default class ProfileScreen extends Component{  
  5.     render(){  
  6.         return(  
  7.             <View>  
  8.                 <Text>this is profile screen</Text>  
  9.             </View>  
  10.         )  
  11.     }  
  12. }  
  13. ProfileScreen.navigationOptions={  
  14.     tabBarIcon:({tintColor, focused})=>(  
  15.         <Icon  
  16.             name={focused ? 'ios-person' : 'md-person'}  
  17.             color={tintColor}  
  18.             size={25}  
  19.         />  
  20.     )  
  21. }  
src/screens/settings.js

  1. import React, {Component} from 'react';  
  2. import {View,Text} from 'react-native';  
  3. import Icon from 'react-native-vector-icons/Ionicons';  
  4. export default class SettingScreen extends Component{  
  5.     render(){  
  6.         return(  
  7.             <View>  
  8.                 <Text>this is setting screen</Text>  
  9.             </View>  
  10.         )  
  11.     }  
  12. }  
  13. SettingScreen.navigationOptions={  
  14.     tabBarIcon:({tintColor, focused})=>(  
  15.         <Icon  
  16.             name={focused ? 'ios-settings' : 'md-settings'}  
  17.             color={tintColor}  
  18.             size={25}  
  19.         />  
  20.     )  
  21. }  
src/lib/router.js

In router.js file, import createMaterialTopTabNavigator and createAppContainer functions of 'react-navigation' library. Also import all the routers classes in it and place them in such sequence as we want to display them on the top of tab navigator.
  • activeTintColor: sets the mention color to the active router.
  • showIcon: show {true} and hide {false} the icon of routers.
  • showLabel: show {true} and hide {false} the title of routers. By default it is true.
  1. import React from 'react';  
  2. import {createMaterialTopTabNavigator,createAppContainer} from 'react-navigation';  
  3. import HomeScreen from "../screens/index";  
  4. import ProfileScreen from "../screens/profile";  
  5. import SettingScreen from "../screens/settings";  
  6.   
  7. const AppNavigator = createMaterialTopTabNavigator(  
  8.     {  
  9.         Home: HomeScreen,  
  10.         Profile: ProfileScreen,  
  11.         Settings: SettingScreen,  
  12.     },  
  13.     {  
  14.         tabBarOptions: {  
  15.             activeTintColor: 'white',  
  16.             showIcon: true,  
  17.             showLabel:false,  
  18.             style: {  
  19.                 backgroundColor:'red'  
  20.             }  
  21.         },  
  22.     }  
  23. )  
  24. export default createAppContainer(AppNavigator);  
src/index.js

Import AppNavigator from './lib/router' and assign the AppNavigator in a const AppIndex in this file. Customize the status bar using StatusBar tag and add header on the top of tab navigator.

  1. import React, {Component} from 'react';  
  2. import {StyleSheet, Text, View,StatusBar} from 'react-native';  
  3. import {createAppContainer} from 'react-navigation';   
  4. import Icon from 'react-native-vector-icons/Ionicons';  
  5.   
  6. import AppNavigator from './lib/router';  
  7. const AppIndex = createAppContainer(AppNavigator)  
  8.   
  9. export default class App extends Component{  
  10.     render(){  
  11.         return(  
  12.             <View style={{flex:1}} >  
  13.                 <StatusBar  
  14.                     backgroundColor='red'  
  15.                     barStyle='light-content'  
  16.                 />  
  17.                 <View style={styles.header}>  
  18.                     <Icon name='ios-camera' size={28} color='white'/>  
  19.                     <Icon name='ios-menu' size={28} color='white'/>  
  20.                 </View>  
  21.                 <AppIndex/>  
  22.             </View>  
  23.         )  
  24.     }  
  25. }  
  26. const styles = StyleSheet.create({  
  27.     wrapper: {  
  28.         flex: 1,  
  29.     },  
  30.     header:{  
  31.         flexDirection: 'row',  
  32.         alignItems: 'center',  
  33.         justifyContent: 'space-between',  
  34.         backgroundColor: 'red',  
  35.         paddingHorizontal: 18,  
  36.         paddingTop: 5,  
  37.     }  
  38. });  

Output:

React Native Top Tab Navigator

Comments

Popular Posts

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...