Introduction :
In this post, I will show you how to add one image as the header title using react native navigation library.
In most cases, we use text as the title for a screen. But, we might need to use one image or logo to display instead of a title in some cases. Thankfully, the react-navigation library provides an easy way to do that, and without doing any major code change, we can achieve it.
Example program :
To use one Image as the header image, we need to pass it as the headerTitle. We can pass one function to headerTitle props that return one Image which will be used as the header image. We can give the height and width of the title image to make it fit in the header.
Let’s take a look at the example screen below :
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{
headerTitle: () => (
<Image style={{ width: 50, height: 50 }} source={require("./logo.png")} />
),
}}
/>
logo.png is placed on the root folder with App.js and we are using the Stack.Navigator in App.js.
If you run this program, it will give the below result :
logo.png is the robot image that is showing in the header.
Height and widths are important :
In the above example, we are providing one height and one width to the image. If we don’t provide any of these, it will not automatically adjust it. You will get the below output if you remove the style props :
It distorted the image.
Placing the image on the left and right end :
headerTitle placed the logo in the center. If you want to place it on left, you can use headerLeft or if you want it on right, you can use headerRight.
For the below change :
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{
headerLeft: () => (
<Image
style={{ width: 30, height: 30, margin: 20 }}
source={require("./robot.png")}
/>
),
}}
/>
Similarly, you can use headerRight to place the logo to the right end of the header.
You might also like:
- React navigation Part 1: How to add react navigation 5.0 in React Native project
- React navigation part 2: Manage multiple screens
- React Navigation part 3: program to move between screens using react navigation
- React Navigation part 4: React native program to pass data between screens
- React Navigation part 5: How to update params of a screen
- React Navigation part 6: How to set and change the header title