简体   繁体   English

React Native Navigation 中标题栏的边距顶部

[英]Margin Top for Header Bar in React Native Navigation

I'm new in React Native.我是 React Native 的新手。 I'm using React-Navigation.我正在使用 React-Navigation。 But, on my device has problem with header navigation.但是,在我的设备上,标题导航有问题。 This overlay by status bar like this.像这样的状态栏覆盖。

在此处输入图片说明

This issue occur both on my code and from React Navigation showcase example.这个问题出现在我的代码和 React Navigation 展示示例中。 How to fix it?如何解决? Thanks谢谢

You are using Expo, so this behavior is normal.您正在使用 Expo,因此这种行为是正常的。

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:有了 Expo,您现在可以使用它:

import Constants from 'expo-constants'

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

Install it with expo install expo-constants使用expo install expo-constants安装它

More informations here in the expo doc世博文档中的更多信息

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只需在配置对象中添加 headerMode: 'none'

Hope this helps希望这可以帮助

By the way Credit goes to This Link顺便说一句信用去这个链接

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
  }
}

If you are using expo, try to set the navigation options like this如果您使用的是 expo,请尝试像这样设置导航选项

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

With this the padding will only take affect in android platform.有了这个,填充只会在android平台上生效。 For more info you can visit link.有关更多信息,您可以访问链接。

In my case, StatusBar will cause this problem.就我而言, StatusBar会导致此问题。

to fix that:解决这个问题:

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

If you are using Expo,you can use Platform from expo core so :如果您使用的是 Expo,则可以使用expo core Platform ,因此:

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
   }
});

With Expo, you could use Constants:使用 Expo,您可以使用常量:

import Constants from 'expo-constants';

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

You can also use the StatusBar component from ReactNative:你也可以使用 ReactNative 的 StatusBar 组件:

import { StatusBar } from 'react-native';

const styles = StyleSheet.create({
  container: {
    marginTop: StatusBar.currentHeight
  },
});

For me, it was as simple as include the property " headerStatusBarHeight " with the value I wanted.对我来说,就像包含属性“ headerStatusBarHeight ”和我想要的值一样简单。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM