【发布时间】:2017-07-18 15:10:29
【问题描述】:
我是 React Native 的新手。我正在使用反应导航。但是,在我的设备上,标题导航有问题。像这样按状态栏叠加。
这个问题出现在我的代码和 React Navigation 展示示例中。如何解决?谢谢
【问题讨论】:
-
感谢@MichaelCheng。重复了
标签: javascript react-native react-navigation
我是 React Native 的新手。我正在使用反应导航。但是,在我的设备上,标题导航有问题。像这样按状态栏叠加。
这个问题出现在我的代码和 React Navigation 展示示例中。如何解决?谢谢
【问题讨论】:
标签: javascript react-native react-navigation
您使用的是 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安装它
【讨论】:
marginTop: StatusBar.currentHeight
currentHeight (Android only) The height of the status bar。因此,该解决方案似乎不适用于 iOS。
import Constants from 'expo-constants'
我发现这在遇到相同问题时很有用
export default StackNavigator({
LoginScreen: { screen: Login.component }
}, {
initialRouteName: 'LoginScreen',
headerMode: 'none' // <------------- This line
})
只需在配置对象中添加 headerMode: 'none'
希望对你有帮助
顺便说一句,信用转到This Link
【讨论】:
这应该做你想做的。
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
}
}
【讨论】:
如果您使用的是 expo,请尝试像这样设置导航选项
navigationOptions:{
headerStyle:{
marginTop: (Platform.OS === 'ios') ? 0 : Expo.Constants.statusBarHeight }
}
}
这样填充只会在android平台上生效。 欲了解更多信息,您可以访问link.
【讨论】:
对我来说,只需将属性“headerStatusBarHeight”包含在我想要的值中即可。
const defaultHeaderConfig = {
headerStatusBarHeight: 20,
headerTintColor: "white",
headerStyle:{
backgroundColor: "blue"
}
}
【讨论】:
在我的情况下,StatusBar 会导致这个问题。
解决这个问题:
<StatusBar
animated={true}
backgroundColor={Styles.statusBar.color}
barStyle={barStyle}
hidden={false}
networkActivityIndicatorVisible={true}
showHideTransition="fade"
translucent={false} // <----------------- add false to translucent
/>
【讨论】:
如果你使用 Expo,你可以使用 Platform from expo core so :
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
}
});
【讨论】:
借助 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
},
});
【讨论】: