Inaccessible
Inaccessible

Reputation: 1560

React native - Hide status bar only on specific screens

I am using tab navigation for uploading images like below

const Photos = TabNavigator({
    CAMERA: {
            screen: TakeCamera,
        navigationOptions: {
          tabBarIcon: ({focused}) => (
            <View style={{flexDirection: 'row'}}>
              <Text style={{textAlign: 'center', color: focused? '#C7A985' : '#020202'}}>CAMERA</Text>
              <Icon name="chevron-down" size={15} color= {focused? '#C7A985' : '#ffffff'}/>
            </View>
          )
        },
        },
      ALBUMS: {
            screen: Albums,
        navigationOptions: {
          tabBarIcon: ({focused}) => (
            <View style={{flexDirection: 'row'}}>
              <Text style={{textAlign: 'center', color: focused? '#C7A985' : '#020202'}}>ALBUMS</Text>
              <Icon name="chevron-down" size={15} color= {focused? '#C7A985' : '#ffffff'}/>
            </View>
          )
        },
        },
    {
      tabBarOptions: {
        upperCaseLabel: false,
        showIcon: true,
        showLabel: false,
        style: {
          backgroundColor: '#F7F1ED',
          borderTopWidth: 1
        }
      },
        //initialRouteName: 'Feed',
      tabBarComponent: TabBarBottom,
      tabBarPosition: 'bottom',
      animationEnabled: false,
      swipeEnabled: false,
    });

export default class UploadPost extends Component {
  static navigationOptions = ({ navigation }) => ({
    header: null,
    tabBarVisible: false
  });

  render() {
    return (
      <View style={{flex: 1}}>
        <StatusBar hidden={true}/>
        <Photos screenProps={{navigation: this.props.navigation}}/>
      </View>
    );
  }
}

Here <Statusbar hidden={true}> hides the status bar in "CAMERA", "ALBUMS" screens as expected. But it also hides the status bar in other screens.

  1. When I open the app, I could see the StatusBar
  2. After I open CAMERA or ALBUMS screens the StatusBar gets hidden permanently for all other screens.

My question is, How to hide status bar only on CAMERA, ALBUMS screens?

Upvotes: 4

Views: 1154

Answers (1)

Pritish Vaidya
Pritish Vaidya

Reputation: 22189

You can use the Screen Tracking Middleware as mentioned in the docs to get the active routeName of the currentScreen

function getActiveRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  // dive into nested navigators
  if (route.routes) {
    return getActiveRouteName(route);
  }
  return route.routeName;
}

If the routeName matches your desired screens in which you don't want to show StatusBar , then set it to true else false

Upvotes: 1

Related Questions