Home > Software design >  React native (expo) have a different navigation bar color for each page (on Android)
React native (expo) have a different navigation bar color for each page (on Android)

Time:05-15

Android phones that have a navigation bar like that from the iPhone, have the default background color as white, which looks really off if my screen's background color is different than white, so I need to change the background color depending on the background color of my screen.

Currently I'm using expo to change that color whenever a screen component loads, but that doesn't work when I'm navigating back to a previous screen.

This is what I have in all of my screens (only with different colors to match my background)

  useEffect(() => {
    if (Platform.OS === 'android') {
      NavigationBar.setBackgroundColorAsync(colors.main);
    }
  }, []);

How can I make this run when I navigate back to a screen that's already loaded? Or what's another solution to my problem?

CodePudding user response:

You should be able to achieve this by importing StatusBar from React-Native;

import { StatusBar } from 'react-native';

const HomeScreen = ({ navigation }) => {
StatusBar.setBackgroundColor(primary_color)
return (
...
)}

Or you can set it within the return function as such;

return (
<SafeAreaView style={styles.container}>
  <StatusBar
    animated={true}
    backgroundColor="red"
    barStyle={statusBarStyle}
    showHideTransition={statusBarTransition}
    hidden={hidden} />
    ...

Ref: https://reactnative.dev/docs/statusbar

CodePudding user response:

What I ended up doing is using useIsFocused from @react-navigation/native to tell when the user came back to the screen and then calling NavigationBar.setBackgroundColorAsync again

  • Related