【问题标题】:iOS (React native): Unnecessary space from the top of the header rendered using react navigationiOS(React Native):使用反应导航呈现的标题顶部不必要的空间
【发布时间】:2018-08-23 11:07:52
【问题描述】:

路由配置

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

为清楚起见添加屏幕截图:

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

P.S我已经尝试将高度设置为headerStyle

【问题讨论】:

    标签: javascript ios reactjs react-native react-navigation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      就我而言,headerMode: 'none' 解决了这个问题。可能会有所帮助

      const Routes = createStackNavigator(
          {
            Login: { screen: Login,  },
      
            // Profile: { screen: ProfileScreen },
          },
          {
              // initialRouteName: 'Login',
              headerMode: 'none'
          }
      );
      

      【讨论】:

        【解决方案3】:

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

        更多详情; https://github.com/react-navigation/react-navigation/issues/3184

        【讨论】:

        • 这为我解决了问题。我正在使用嵌套堆栈导航器,第二个标题有这个问题(额外的 paddingTop)。
        • 为我工作。在 ios 上遇到问题
        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-16
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 1970-01-01
          • 2017-02-07
          相关资源
          最近更新 更多