最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - iOS (React native): Unnecessary space from the top of the header rendered using react navigation - Stack Overflow

programmeradmin4浏览0评论

Route Config

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

Adding screenshots for clarity:

How can I center the header content and get rid of the unnecessary space from the top?

P.S I have already tried setting the height to headerStyle

Route Config

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

Adding screenshots for clarity:

How can I center the header content and get rid of the unnecessary space from the top?

P.S I have already tried setting the height to headerStyle

Share Improve this question asked Mar 14, 2018 at 19:07 shet_tayyyshet_tayyy 5,75512 gold badges53 silver badges92 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

Try placing this code in your App.js file:

import { SafeAreaView } from "react-navigation";

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

You can set headerForceInset: { top: 'never', bottom: 'never' } in the navigationOptions and that will remove the paddingTop.

For more details; https://github.com/react-navigation/react-navigation/issues/3184

In my case headerMode: 'none' solved the issue. May be helpful

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

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

If you use StatusBar with translucent options, you need to use in your screenOptions on Stack.Navigator, the option headerStatusBarHeight: 0

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论