21

I'm new in React Native. I'm using React-Navigation. But, on my device has problem with header navigation. This overlay by status bar like this.

enter image description here

This issue occur both on my code and from React Navigation showcase example. How to fix it? Thanks

Hinrich
  • 13,485
  • 7
  • 43
  • 66
Pham Minh Tan
  • 2,076
  • 7
  • 25
  • 37

8 Answers8

15

You are using Expo, so this behavior is normal.

static navigationOptions = {
  title: "Welcome",
  headerStyle: { marginTop: 24 },
}

You can define your header like this.

Edit about a year later:

With Expo, you can now use this:

import Constants from 'expo-constants'

static navigationOptions = {
  title: "Welcome",
  headerStyle: { marginTop: Constants.statusBarHeight },
}

Install it with expo install expo-constants

More informations here in the expo doc

Poptocrack
  • 2,879
  • 1
  • 12
  • 28
  • 2
    This doesn't work as well on iOS. [Accepted answer to the duplicate](https://stackoverflow.com/a/45045132/383414) works better on both platforms i.e. `marginTop: StatusBar.currentHeight` – Richard Le Mesurier Nov 23 '17 at 13:11
  • @RichardLeMesurier According to the [React Native site](https://facebook.github.io/react-native/docs/statusbar.html#constants) `currentHeight (Android only) The height of the status bar`. So that solution doesn't seem to be working on iOS. – Charlie Fish Jan 25 '18 at 21:19
  • @CharlieFish RN changes so quickly, but when I made that comment, I was developing an Expo app on an iPhone 4c and Android Nexus 5 and got a better experience with the duplicate as said. But this is an every-flowing technology we are working on for better or worse. – Richard Le Mesurier Jan 26 '18 at 08:59
  • `import Constants from 'expo-constants'` – Alon Dahari Jun 01 '20 at 17:33
9

I found this useful when running in the same problem

export default StackNavigator({
    LoginScreen: { screen: Login.component }
}, {
    initialRouteName: 'LoginScreen',
    headerMode: 'none' // <------------- This line
})

Just add headerMode: 'none' in the configuration object

Hope this helps

By the way Credit goes to This Link

instanceof
  • 1,404
  • 23
  • 30
Ahmad Khoja
  • 483
  • 6
  • 10
4

This should do what you want.

import {
  StyleSheet,
  View,
  Platform
} from 'react-native';
import { Constants } from 'expo';

const App = () => (
    <View style={styles.container}>
        // Your content with margin for statusBar goes here
    </View>
)

const styles = StyleSheet.create({
  container: {
    marginTop: Platform.OS === 'ios' ? 0 : Constants.statusBarHeight
  }
}
Hinrich
  • 13,485
  • 7
  • 43
  • 66
3

In my case the StatusBar component cause this problem.

set translucent props to false

<StatusBar
        animated={true}
        backgroundColor={Styles.statusBar.color}
        barStyle={barStyle}
        hidden={false}
        networkActivityIndicatorVisible={true}
        showHideTransition="fade"
        translucent={false} // <----------------- add false to translucent
      />

nima
  • 7,796
  • 12
  • 36
  • 53
2

If you are using expo, try to set the navigation options like this

navigationOptions:{
  headerStyle:{
    marginTop: (Platform.OS === 'ios') ? 0 : Expo.Constants.statusBarHeight }
  }
}

With this the padding will only take affect in android platform. For more info you can visit link.

Darren Lau
  • 1,995
  • 4
  • 25
  • 40
2

For me, it was as simple as include the property "headerStatusBarHeight" with the value I wanted.

const  defaultHeaderConfig = {
    headerStatusBarHeight: 20,
    headerTintColor: "white",
    headerStyle:{
        backgroundColor: "blue"
    }
}
0

If you are using Expo,you can use Platform from expo core so :

import { Constants } from "expo";
import { Platform } from "expo-core"; //inside of Platfrom from 'react-native'

after that create an stylesheet:

const styles = StyleSheet.create({
  container: {
   marginTop: Platform.OS === "ios" ? 0 : Constants.statusBarHeight
   }
});
MBehtemam
  • 7,865
  • 15
  • 66
  • 108
0

With Expo, you could use Constants:

import Constants from 'expo-constants';

const styles = StyleSheet.create({
  container: {
    marginTop: Constants.statusBarHeight
  },
});

You can also use the StatusBar component from ReactNative:

import { StatusBar } from 'react-native';

const styles = StyleSheet.create({
  container: {
    marginTop: StatusBar.currentHeight
  },
});
pjehan
  • 820
  • 10
  • 18