Skip to main content

React Native Configuring Header Bar

The static property of a screen component is called navaigationOptions. It is either an object or a function. It returns an object containing several configuration options.

Props of the header bar

PropsDescription
titleIt sets the title of active screen.
headerStyleIt adds style to header bar.
backgroundColorIt sets the background color of the header bar.
headerTintColorIt sets the color to header title.
headerTitleStyleIt adds style to the title of a screen.
fontWeightIt sets the font style of header title.
  1. static navigationOptions = {  
  2.     title: 'HeaderTitle',  
  3.     headerStyle: {  
  4.         backgroundColor: '#f4511e',  
  5.     },  
  6.     headerTintColor: '#0ff',  
  7.     headerTitleStyle: {  
  8.        fontWeight: 'bold',  
  9.     },  
  10. };  

React Native Moving from One Screen to Other Example 1

In this example, we create two screen named as 'Home' and 'Profile'. The Home screen is set as first screen using "initialRouteName" property and Profile screen as second.
App.js
  1. import React from 'react';  
  2. import { View, Text, Button } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     static navigationOptions = {  
  7.         title: 'Home',  
  8.         headerStyle: {  
  9.             backgroundColor: '#f4511e',  
  10.         },  
  11.         //headerTintColor: '#0ff',  
  12.         headerTitleStyle: {  
  13.             fontWeight: 'bold',  
  14.         },  
  15.     };  
  16.   
  17.     render() {  
  18.         return (  
  19.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  20.                 <Text>Home Screen</Text>  
  21.                 <Button  
  22.                     title="Go to Profile"  
  23.                     onPress={() => this.props.navigation.push('Profile')}  
  24.                 />  
  25.             </View>  
  26.         );  
  27.     }  
  28. }  
  29. class ProfileScreen extends React.Component {  
  30.     static navigationOptions = {  
  31.         title: 'Profile',  
  32.         headerStyle: {  
  33.             backgroundColor: '#f4511e',  
  34.         },  
  35.         headerTintColor: '#0ff',  
  36.         headerTitleStyle: {  
  37.             fontWeight: 'bold',  
  38.         },  
  39.     };  
  40.     render() {  
  41.         return (  
  42.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  43.                 <Text>Profile Screen</Text>  
  44.                 <Button  
  45.                     title="Go to Profile... again"  
  46.                     onPress={() => this.props.navigation.push('Profile')}  
  47.                 />  
  48.                 <Button  
  49.                     title="Go to Home"  
  50.                     onPress={() => this.props.navigation.navigate('Home')}  
  51.                  />  
  52.                 <Button  
  53.                     title="Go back"  
  54.                     onPress={() => this.props.navigation.goBack()}  
  55.                 />  
  56.             </View>  
  57.     );  
  58.     }  
  59. }  
  60.   
  61. const AppNavigator = createStackNavigator(  
  62.     {  
  63.         Home: HomeScreen,  
  64.         Profile: ProfileScreen  
  65.     },  
  66.     {  
  67.         initialRouteName: "Home"  
  68.     }  
  69. );  
  70.   
  71. const AppContainer = createAppContainer(AppNavigator);  
  72. export default class App extends React.Component {  
  73.     render() {  
  74.         return <AppContainer />;  
  75.     }  
  76. }  

Output:
React Native Configuring Header Bar React Native Configuring Header Bar

Using params in the title

To use the params (parameter) as a title, we need to make navigationOptions as a function which returns a configuration object. Use the this.props inside the navigationOptions. As it is the static property of component "this" does not refer to an instance of componen and therefore no props are available.
Making navigationOptions as a function which returns the object containing {navigation, navigationOptions, screenProps }. The navigation is the object which is passed to screen props as this.props.navigation. We can also get the params from navigation using navigation.getParam or navigation.state.params.

  1. class ProfileScreen extends React.Component {  
  2.     static navigationOptions = ({ navigation }) => {  
  3.         return {  
  4.             title: navigation.getParam('otherParam''A Param Header'),  
  5.         };  
  6.     };  
  7. }  

The navigationOtions configuration of the active screen which can also be updated from the current screen component itself.

  1. //inside render   
  2. <Button  
  3.     title="Update the title"  
  4.     onPress={() => this.props.navigation.setParams({otherParam: 'Header Updated!'})}  
  5. />  

Complete code

In this example, we create two screen "Home" and "Profile". The Profile screen set its header title using params as: title: navigation.getParam('otherParam', 'A Param Header')

App.js

  1. import React from 'react';  
  2. import { View, Text, Button } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     static navigationOptions = {  
  7.         title: 'Home',  
  8.         headerStyle: {  
  9.             backgroundColor: '#f4511e',  
  10.         },  
  11.         //headerTintColor: '#0ff',  
  12.         headerTitleStyle: {  
  13.             fontWeight: 'bold',  
  14.         },  
  15.     };  
  16.   
  17.     render() {  
  18.         return (  
  19.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  20.                 <Text>Home Screen</Text>  
  21.                 <Button  
  22.                     title="Go to Profile"  
  23.                     onPress={() => this.props.navigation.push('Profile')}  
  24.                 />  
  25.             </View>  
  26.         );  
  27.     }  
  28. }  
  29. class ProfileScreen extends React.Component {  
  30.     static navigationOptions = ({ navigation }) => {  
  31.         return {  
  32.             title: navigation.getParam('otherParam''A Param Header'),  
  33.         };  
  34.     };  
  35.   
  36.     render() {  
  37.         return (  
  38.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  39.                 <Text>Profile Screen</Text>  
  40.                 <Button  
  41.                     title="Go back"  
  42.                     onPress={() => this.props.navigation.goBack()}  
  43.                 />  
  44.                 <Button  
  45.                     title="Update the title"  
  46.                     onPress={() => this.props.navigation.setParams({otherParam: 'Header Updated!'})}  
  47.                 />  
  48.             </View>  
  49.     );  
  50.     }  
  51. }  
  52.   
  53. const AppNavigator = createStackNavigator(  
  54.     {  
  55.         Home: HomeScreen,  
  56.         Profile: ProfileScreen  
  57.     },  
  58.     {  
  59.         initialRouteName: "Home"  
  60.     }  
  61. );  
  62.   
  63. const AppContainer = createAppContainer(AppNavigator);  
  64. export default class App extends React.Component {  
  65.     render() {  
  66.         return <AppContainer />;  
  67.     }  
  68. }  
React Native Configuring Header Bar React Native Configuring Header Bar React Native Configuring Header Bar

Comments

Popular Posts

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

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...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...