你正在使用Expo,所以这种行为是正常的。
static navigationOptions = {
title: "Welcome",
headerStyle: { marginTop: 24 },
}
你可以像这样定义你的标题。
大约一年后编辑:
使用 Expo,现在你可以这样做:import Constants from 'expo-constants'
static navigationOptions = {
title: "Welcome",
headerStyle: { marginTop: Constants.statusBarHeight },
}
使用 expo install expo-constants
进行安装。
更多信息请参见 Expo 文档。
import Constants from 'expo-constants'
- Alon Dahariexport default StackNavigator({
LoginScreen: { screen: Login.component }
}, {
initialRouteName: 'LoginScreen',
headerMode: 'none' // <------------- This line
})
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
}
}
StatusBar
组件引起了这个问题。translucent
属性设置为 false
。<StatusBar
animated={true}
backgroundColor={Styles.statusBar.color}
barStyle={barStyle}
hidden={false}
networkActivityIndicatorVisible={true}
showHideTransition="fade"
translucent={false} // <----------------- add false to translucent
/>
对我来说,这很简单,只需包含属性"headerStatusBarHeight"和我想要的值即可。
const defaultHeaderConfig = {
headerStatusBarHeight: 20,
headerTintColor: "white",
headerStyle:{
backgroundColor: "blue"
}
}
navigationOptions:{
headerStyle:{
marginTop: (Platform.OS === 'ios') ? 0 : Expo.Constants.statusBarHeight }
}
}
使用此方法,填充仅会在安卓平台上生效。 如需更多信息,请访问链接。
使用Expo,您可以使用常量:
import Constants from 'expo-constants';
const styles = StyleSheet.create({
container: {
marginTop: Constants.statusBarHeight
},
});
你也可以使用ReactNative中的StatusBar组件:
import { StatusBar } from 'react-native';
const styles = StyleSheet.create({
container: {
marginTop: StatusBar.currentHeight
},
});
expo core
中使用Platform
:import { Constants } from "expo";
import { Platform } from "expo-core"; //inside of Platfrom from 'react-native'
然后创建一个样式表:
const styles = StyleSheet.create({
container: {
marginTop: Platform.OS === "ios" ? 0 : Constants.statusBarHeight
}
});