Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Margin Top for Header Bar in React Native Navigation

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

like image 454
Pham Minh Tan Avatar asked Jul 18 '17 15:07

Pham Minh Tan


4 Answers

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

like image 115
Poptocrack Avatar answered Nov 05 '22 19:11

Poptocrack


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

like image 9
Ahmad Khoja Avatar answered Nov 05 '22 20:11

Ahmad Khoja


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
  }
}
like image 4
Hinrich Avatar answered Nov 05 '22 20:11

Hinrich


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.

like image 2
Darren Lau Avatar answered Nov 05 '22 19:11

Darren Lau