6

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

这是我最初使用的模板,使用新的 Expo 应用程序附带的模板

这就是标题为透明时的样子。这基本上是我想看到的,但标题仍然被强制在那里。

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

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

这是我的 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'
  }
}
4

4 回答 4

22

在您的方案中,您有两个选择。您可以禁用所有屏幕的标题,也可以仅禁用选定屏幕的标题。

要禁用全方位应用程序的标头,请像这样编辑您的 app.js

应用程序.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并使headerShown:false

假设您只想在特定屏幕上禁用标题,那么此示例将对您有所帮助

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

希望你对此有清楚的想法:)

于 2020-02-28T03:45:58.010 回答
1

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

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

另外,我认为您也可以将 screenOptions 的 headerShown 道具设置为 false 作为 defaultProp,但这就像在每个屏幕上隐藏标题而不是只做一次。

于 2020-10-14T20:34:07.620 回答
0

在反应导航 V5 中,添加options={{ title: "Sign Up", animationEnabled: false, headerShown: false }}

  <AuthStack.Screen
  name="SignupScreen"
  component={SignupScreen}
  options={{ title: "Sign Up", animationEnabled: false, headerShown: false }}
/>
于 2020-08-28T18:45:49.057 回答
0

一屏删除:

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

在所有堆栈中删除:

     <Stack.Navigator
        screenOptions={{
         headerShown: false
        }}
        >
于 2021-08-04T17:47:20.947 回答