iOS(react-native):使用react导航呈现的标题顶部的不必要空间

IT技术 javascript ios reactjs react-native react-navigation
2021-05-27 14:49:54

路由配置

/**
 * 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;

为清楚起见添加屏幕截图: 如您所见,标题内容未居中对齐 标题高度为64 绝对定位的标题内容在底部

如何将标题内容居中并从顶部去除不必要的空间?

PS我已经尝试将高度设置为headerStyle

4个回答

尝试将此代码放在您的 App.js 文件中:

import { SafeAreaView } from "react-navigation";

if (Platform.OS === "android") {
  // removes extra space at top of header on android
  SafeAreaView.setStatusBarHeight(0);
}

您可以headerForceInset: { top: 'never', bottom: 'never' }navigationOptions 中进行设置这将删除 paddingTop。

更多细节; https://github.com/react-navigation/react-navigation/issues/3184

在我的情况下headerMode: 'none'解决了这个问题。可能会有所帮助

const Routes = createStackNavigator(
    {
      Login: { screen: Login,  },

      // Profile: { screen: ProfileScreen },
    },
    {
        // initialRouteName: 'Login',
        headerMode: 'none'
    }
);

如果使用带有半透明选项的 StatusBar,则需要在 Stack.Navigator 上的 screenOptions 中使用选项 headerStatusBarHeight: 0

<Stack.Navigator
  screenOptions={{
    headerStatusBarHeight: 0,
  }}
>