3 Ways to Add Image Icon Inside Navigation Bar in React Native

3 Ways to Add Image Icon Inside Navigation Bar

Here are the 3 Ways to Add Image Icon Inside Navigation Bar in React Native. If you want to make an application with a React Navigation, you can find a NavigationBar/ ActionBar on the top of the screen.

If you want to add the image/logo in Navigation Bar you can simply do it using headerLeft: ()=> <You Image component/>. To add image Icon inside Navigation Bar here are the following options.

Options to Add Image Icon Inside Navigation Bar

1. For the single screen, you can set the custom header in navigationOptions

static navigationOptions = {
  //To set the header image and title for the current Screen
  title: 'Home Activity',
  headerLeft: ()=> <ActionBarImage />,
  headerStyle: {
    backgroundColor: '#e3e3e3',
  },
  headerTintColor: '#606070',
};

2. For the single screen, you can set the custom header in createStackNavigator like this

const App = createStackNavigator(
  {
    First: {
      screen: HomeActivity,
      //You can set the header image and title for the screen from here also
      navigationOptions: {
        title: 'Home Activity',
        headerLeft: ()=> <ActionBarImage />,
        headerStyle: {
          backgroundColor: '#e3e3e3',
        },
        headerTintColor: '#606070',
      }
    },
  }
);

3. Set custom header in all the screens in once using defaultNavigationOptions

const App = createStackNavigator(
  {
    First: {
      screen: HomeActivity
    },
  }
  //You can set the header image and title for all the screens in once using defaultNavigationOptions
  ,{
    defaultNavigationOptions: {
      title: 'Application Name',
      headerLeft: ()=> <ActionBarImage />,
      headerStyle: {
        backgroundColor: '#e3e3e3',
      },
      headerTintColor: '#606070',
    },
  }
);

In this example, We will create an ActionBarImage component which can be set easily from anywhere if we follow any option from above. We will set Image/Icon on the left side with header title, header background and text color of the title. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To switch between the screens and to make a navigation header we need to add react-navigation in our application.

To install the dependencies open the terminal and jump into your project

cd ProjectName

1. Install react-navigation dependency to import createAppContainer

npm install react-navigation --save

2. Other supporting libraries for react-navigation

npm install react-native-gesture-handler react-native-safe-area-context @react-native-community/masked-view react-native-screens react-native-reanimated --save

3. Install react-navigation-stack to import createStackNavigator

npm install react-navigation-stack --save

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

Please use the following command to install CocoaPods

cd ios && pod install && cd ..

Project File Structure

To start with this example you need to create a directory named pages in your project and create two files HomeActivity.js and ActionBarImage.js in it.

Code

Now open App.js in any code editor and replace the code with the following code

App.js

//This is an example to add Image Icon Inside Navigation Bar

import React, { Component } from 'react';
//import react in our code.

//Import react-navigation
import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';

import HomeActivity from './pages/HomeActivity';
import ActionBarImage from './pages/ActionBarImage';

const App = createStackNavigator(
  {
    First: {
      screen: HomeActivity,
      //You can set the header image and title for the screen from here also
      /*navigationOptions: {
        title: 'Home Activity',
        headerLeft: ()=> <ActionBarImage />,
        headerStyle: {
          backgroundColor: '#e3e3e3',
        },
        headerTintColor: '#606070',
      }*/
    },
  }
  //You can set the header image and title for all the screens in once using defaultNavigationOptions
  /*,{
    defaultNavigationOptions: {
      title: 'Application Name',
      headerLeft: ()=> <ActionBarImage />,
      headerStyle: {
        backgroundColor: '#e3e3e3',
      },
      headerTintColor: '#606070',
    },
  }*/
);
export default createAppContainer(App);

Open pages/HomeActivity.js in any code editor and replace the code with the following code.

HomeActivity.js

import React, { Component } from 'react';

import { StyleSheet, View, Text, Image } from 'react-native';

import ActionBarImage from './ActionBarImage'

export default class HomeActivity extends Component {
  static navigationOptions = {
    //To set the header image and title for the current Screen
    title: 'Home Activity',
    //Title
    headerLeft: <ActionBarImage />,
    //Image in Navigation Bar

    headerStyle: {
      backgroundColor: '#e3e3e3',
      //Background Color of Navigation Bar
    },

    headerTintColor: '#606070',
    //Text Color of Navigation Bar
  };

  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 20 }}>Image Icon Inside Header Title Bar</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    padding: 10,
    backgroundColor: '#fff',
  },
});

Open pages/ActionBarImage.js in any code editor and replace the code with the following code.

ActionBarImage.js

import React, { Component } from 'react';

import { StyleSheet, View, Text, Image } from 'react-native';

export default class ActionBarImage extends Component {
  render() {
    return (
      <View style={{ flexDirection: 'row' }}>
        <Image
          source={{
            uri:
              'https://raw.githubusercontent.com/AboutReact/sampleresource/master/logosmalltransparen.png',
          }}
          style={{
            width: 40,
            height: 40,
            borderRadius: 40 / 2,
            marginLeft: 15,
          }}
        />
      </View>
    );
  }
}

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

IOS

Android

Output in Online Emulator

This is how you can set Image Icon inside Navigation Bar. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂


Related Posts

6 thoughts on “3 Ways to Add Image Icon Inside Navigation Bar in React Native”

  1. I got error
    below one please look at once

    bundling failed: Error: Unable to resolve module `react-navigation` from `App.js`: react-navigation could not be found within the project or in these directories:
    node_modules

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.