iOS(React native):使用 React 导航呈现的 header 顶部不必要的 space
iOS (React native): Unnecessary space from the top of the header rendered using react navigation
路由配置
/**
* Author: Rahul
* Date: 25 Feb 2018
*
* Routes
* @flow
*/
import React from 'react';
import { View, Text } from 'react-native';
import { StackNavigator } from 'react-navigation';
import LoginScreen from 'src/containers/login';
import HomeScreen from 'src/containers/home';
import FeedsScreen from 'src/containers/feeds';
import { AppLogo } from 'src/components';
import { background } from 'src/styles/';
import { SIGNED_IN, SIGNED_OUT, HOME, LOGIN, FEEDS } from './constants';
const navigationOptions = {
navigationOptions: {
headerLeft: (
<View>
<Text>Hamburger</Text>
</View>
),
headerRight: (
<AppLogo />
),
headerStyle: {
paddingHorizontal: 16,
backgroundColor: background.color2,
},
gesturesEnabled: false,
},
};
const SignedOutRouteConfig = {
[LOGIN]: { screen: LoginScreen },
};
const SignedInRouteConfig = {
[HOME]: { screen: HomeScreen },
[FEEDS]: { screen: FeedsScreen },
};
const SignedOut = StackNavigator(SignedOutRouteConfig, navigationOptions);
const SignedIn = StackNavigator(SignedInRouteConfig, navigationOptions);
const createRootNavigator = (signedIn: boolean = false) => StackNavigator(
{
[SIGNED_IN]: {
screen: SignedIn,
navigationOptions: {
gesturesEnabled: false,
header: null,
},
},
[SIGNED_OUT]: {
screen: SignedOut,
navigationOptions: {
gesturesEnabled: false,
header: null,
},
},
},
{
initialRouteName: signedIn ? SIGNED_IN : SIGNED_OUT,
}
);
export default createRootNavigator;
为清楚起见添加屏幕截图:
如何将 header 内容居中并从顶部删除不必要的 space?
P.S 我已经尝试将高度设置为 headerStyle
尝试将此代码放入您的 App.js 文件中:
import { SafeAreaView } from "react-navigation";
if (Platform.OS === "android") {
// removes extra space at top of header on android
SafeAreaView.setStatusBarHeight(0);
}
在我的案例中 headerMode: 'none'
解决了这个问题。可能会有帮助
const Routes = createStackNavigator(
{
Login: { screen: Login, },
// Profile: { screen: ProfileScreen },
},
{
// initialRouteName: 'Login',
headerMode: 'none'
}
);
您可以在 navigationOptions 中设置 headerForceInset: { top: 'never', bottom: 'never' }
,这将删除 paddingTop。
了解更多详情;
https://github.com/react-navigation/react-navigation/issues/3184
如果你使用带有半透明选项的 StatusBar,你需要在 Stack.Navigator 上的 screenOptions 中使用选项 headerStatusBarHeight: 0
<Stack.Navigator
screenOptions={{
headerStatusBarHeight: 0,
}}
>
路由配置
/**
* Author: Rahul
* Date: 25 Feb 2018
*
* Routes
* @flow
*/
import React from 'react';
import { View, Text } from 'react-native';
import { StackNavigator } from 'react-navigation';
import LoginScreen from 'src/containers/login';
import HomeScreen from 'src/containers/home';
import FeedsScreen from 'src/containers/feeds';
import { AppLogo } from 'src/components';
import { background } from 'src/styles/';
import { SIGNED_IN, SIGNED_OUT, HOME, LOGIN, FEEDS } from './constants';
const navigationOptions = {
navigationOptions: {
headerLeft: (
<View>
<Text>Hamburger</Text>
</View>
),
headerRight: (
<AppLogo />
),
headerStyle: {
paddingHorizontal: 16,
backgroundColor: background.color2,
},
gesturesEnabled: false,
},
};
const SignedOutRouteConfig = {
[LOGIN]: { screen: LoginScreen },
};
const SignedInRouteConfig = {
[HOME]: { screen: HomeScreen },
[FEEDS]: { screen: FeedsScreen },
};
const SignedOut = StackNavigator(SignedOutRouteConfig, navigationOptions);
const SignedIn = StackNavigator(SignedInRouteConfig, navigationOptions);
const createRootNavigator = (signedIn: boolean = false) => StackNavigator(
{
[SIGNED_IN]: {
screen: SignedIn,
navigationOptions: {
gesturesEnabled: false,
header: null,
},
},
[SIGNED_OUT]: {
screen: SignedOut,
navigationOptions: {
gesturesEnabled: false,
header: null,
},
},
},
{
initialRouteName: signedIn ? SIGNED_IN : SIGNED_OUT,
}
);
export default createRootNavigator;
为清楚起见添加屏幕截图:
如何将 header 内容居中并从顶部删除不必要的 space?
P.S 我已经尝试将高度设置为 headerStyle
尝试将此代码放入您的 App.js 文件中:
import { SafeAreaView } from "react-navigation";
if (Platform.OS === "android") {
// removes extra space at top of header on android
SafeAreaView.setStatusBarHeight(0);
}
在我的案例中 headerMode: 'none'
解决了这个问题。可能会有帮助
const Routes = createStackNavigator(
{
Login: { screen: Login, },
// Profile: { screen: ProfileScreen },
},
{
// initialRouteName: 'Login',
headerMode: 'none'
}
);
您可以在 navigationOptions 中设置 headerForceInset: { top: 'never', bottom: 'never' }
,这将删除 paddingTop。
了解更多详情; https://github.com/react-navigation/react-navigation/issues/3184
如果你使用带有半透明选项的 StatusBar,你需要在 Stack.Navigator 上的 screenOptions 中使用选项 headerStatusBarHeight: 0
<Stack.Navigator
screenOptions={{
headerStatusBarHeight: 0,
}}
>