0

I know I should be providing code, but at this time, none of my StatusBar code is active and the color is defaulting to the "background color" correctly for my initial sign in screen. I had my <StatusBar /> component at the top level of my app, but now, I need to change StatusBar color based on the route.
After Reading the docs, it looks straight-foward enough since I am using a Stack nav. I can leave in my top level & then add it as a nested StatusBar which gets rendered. However, the color doesn't change at all.

Inside my ScreenView, i am adding:

      <StatusBar barStyle={'default'} backgroundColor={'green'} />

So I ripped out all of my StatusBar references, and the color still seems to be correct out of the box ‍♂️ on the initial page view route.

Any chance the SafeAreaProvider from Expo could be impacting it?

How else could the Status bar color be impacted?

I am using Expo SDK 46 + React Native 0.69.6

GIF: enter image description here

Something this simple couldn't be tricky to implement right?

Phil Lucks
  • 2,704
  • 6
  • 31
  • 57

2 Answers2

1

use below given code snippet for changing status bar color on each forward and backward move of screens -

Android only solution :

import {useFocusEffect} from '@react-navigation/native';

//inside your functional component use useFocusEffect hook

useFocusEffect(
    React.useCallback(() => {
        StatusBar.setBarStyle('dark-content'); // 'light-content' is also available
         StatusBar.setBackgroundColor('white'); //add color code
        StatusBar.setTranslucent(true);
    }, []),
  );
Kailash
  • 777
  • 4
  • 19
  • I am getting warnings that setBackgroundColor, setTranslucent are only available on Android – Phil Lucks Sep 28 '22 at 14:39
  • Yes, setTranslucent will only work with Android. Either you remove it or ignore the warning. The first two lines regarding setting up Status bar will work for your purpose. – Kailash Sep 28 '22 at 14:43
  • setBackgroundColor also is for android only? Based on their TS file – Phil Lucks Sep 28 '22 at 14:44
  • 1
    https://stackoverflow.com/questions/39297291/how-to-set-ios-status-bar-background-color-in-react-native hope this will be useful for iOS. – Kailash Sep 28 '22 at 14:49
  • thanks i also think the Expo Status bar is not yet supported in SDK 46: https://github.com/expo/expo/tree/sdk-46/packages/expo-status-bar#installation-in-managed-expo-projects – Phil Lucks Sep 28 '22 at 15:07
  • link above worked, thanks! – Phil Lucks Sep 28 '22 at 17:44
-1

I was never able to get that method working with Expo. Fortunately the imperative method still works:

import { StatusBar } from 'react-native';
StatusBar.setBackgroundColor('green');
StatusBar.setBarStyle(isDark ? 'light-content' : 'dark-content');
Slbox
  • 10,957
  • 15
  • 54
  • 106