删除 Header React Navigation v5

Remove Header React Navigation v5

我似乎无法在新版本的 React Navigation 中将 header 配置为 null。我可以使用 headerTransparent 选项将其设置为透明,但这看起来 header 仍然存在,因为屏幕仍然需要一个名称。

Here is what I had initially, using the template that comes with a new Expo application

And this is what it looks like with the header as transparent。这基本上是我想看到的,但标题仍然是强制性的。

我不想 header 我的导航,但这看起来像是默认行为。我尝试查看文档以查看是否有删除 header 的道具,但遇到了 404 选项页面:https://reactnavigation.org/docs/en/navigation-options.html

我是 React Native 的新手,所以我可能有些误解。但是文档对此并不清楚,我找不到直接解决这个问题的 Whosebug 问题。

这是我的 App.js

import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

import BottomTabNavigator from './navigation/BottomTabNavigator';
import useLinking from './navigation/useLinking';

const Stack = createStackNavigator();

........

<NavigationContainer ref={containerRef} initialState={initialNavigationState}>
  <Stack.Navigator>
    <Stack.Screen name="root" component={BottomTabNavigator} options={{headerTransparent: true}}/>
  </Stack.Navigator>
</NavigationContainer>

这里是我的BottomTabNavigator.js,和expo提供的模板代码很像。

import * as React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import TabBarIcon from '../components/TabBarIcon';
import HomeScreen from '../screens/Home';
import SearchScreen from '../screens/Search';

const BottomTab = createBottomTabNavigator();
const INITIAL_ROUTE_NAME = 'Home';

export default function BottomTabNavigator({ navigation, route }) {
  navigation.setOptions({ headerTitle: getHeaderTitle(route) });
  return (
    <BottomTab.Navigator initialRouteName={INITIAL_ROUTE_NAME}>
      <BottomTab.Screen
        name="Home"
        component={HomeScreen}
        options={{
          title: 'Home',
          tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-home" />
        }}
      />
      <BottomTab.Screen
        name="Search"
        component={SearchScreen}
        options={{
          title: 'Search',
          tabBarIcon: ({ focused }) => <TabBarIcon focused={focused} name="md-search" />,
        }}
      />
    </BottomTab.Navigator>
  );
}

function getHeaderTitle(route) {
  const routeName = route.state?.routes[route.state.index]?.name ?? INITIAL_ROUTE_NAME;

  switch (routeName) {
    case 'Home':
      return 'How to get started';
    case 'Appointments':
      return 'Your appointments';
    case 'Search':
      return 'Search for services';
    case 'Account':
      return 'Account'
  }
}

在您的场景中,您有两种选择。您可以为所有屏幕禁用 header,也可以仅为所选屏幕禁用 header。

要为 all-around 禁用 header 你的应用程序像这样编辑你的 app.js

App.js

import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

import BottomTabNavigator from './navigation/BottomTabNavigator';
import useLinking from './navigation/useLinking';

const Stack = createStackNavigator();

........

<NavigationContainer ref={containerRef} initialState={initialNavigationState}>
  <Stack.Navigator screenOptions={{headerShown: false,}}>
    <Stack.Screen name="root" component={BottomTabNavigator}/>
  </Stack.Navigator>
</NavigationContainer>

需要将screenOptions传入Stack.Navigator,使header显示:假

假设您只想在特定屏幕上禁用 header,那么此示例将帮助您

<Stack.Navigator ...>
 ...
  <Stack.Screen
    name="Landing"
    component={LandingScreen}
    options={{
      headerShown: false, // change this to `false`
    }}
  />
...
</Stack.Navigator>

希望你对此有清晰的认识:)

在 React Navigation V5 中,添加 options={{ title: "Sign Up", animationEnabled: false, headerShown: false }}

  <AuthStack.Screen
  name="SignupScreen"
  component={SignupScreen}
  options={{ title: "Sign Up", animationEnabled: false, headerShown: false }}
/>

headerMode: none 设置为默认道具会将其从任何屏幕中移除。

const Stack = createStackNavigator();
Stack.Navigator.defaultProps = {
    headerMode: 'none',
};

此外,我认为您也可以将 screenOptions 的 headerShown 属性设置为 false 作为 defaultProp,但这就像在每个屏幕上隐藏 header 而不是一次隐藏一样。

一屏删除:

      <Stack.Screen
              name="SignIn"
              component={SignInScreen}
              options={{
                headerShown: false,
              }}
            />

在所有堆栈中移除:

     <Stack.Navigator
        screenOptions={{
         headerShown: false
        }}
        >