简体   繁体   English

React-navigation:使用底部选项卡导航器时不显示标题

[英]React-navigation: header does not show up when using bottom tab navigator

I am using react-navigation with my react native app.我正在将react-navigation与我的本机应用程序一起使用。 I have created a bottom tab navigator, and want to use the built in header on my screen.我创建了一个底部选项卡导航器,并希望在我的屏幕上使用内置标题。 But the header is not showing up.但是标题没有出现。 There are no errors or warnings.没有错误或警告。

app.js:应用程序.js:

const TabStack = createBottomTabNavigator({
  Upload: { 
    screen: upload,
    navigationOption: {
      headerTitle: "Upload"
    }
  },
  Profile: { 
    screen: profile,
    navigationOption: {
      headerTitle: "Profile"
    }
  }
});

const MainStack = createSwitchNavigator(
  {
    Home: { screen: TabStack }
  },
  {
    initialRouteName: 'Home'
  }
);

upload.js上传.js

class upload extends React.Component {
    static navigationOptions = {
        title: 'Upload'
    };

    constructor(props) {
        super(props);

    ...

I know declaring navigationOptions in the components is probably not needed since it is already declared in app.js but this is just to show that neither approach works.我知道在组件中声明navigationOptions可能是不需要的,因为它已经在 app.js 中声明了,但这只是为了表明这两种方法都不起作用。

How do I fix this?我该如何解决?

TabNavigator is not shipped with a Header . TabNavigator不附带Header The most common case is to make your TabNavigator the root navigator, and make each tab a StackNavigator you would then get the header cause it's part of the StackNavigator by default.最常见的情况是让您的TabNavigator成为导航器,并使每个选项卡成为StackNavigator ,然后您将获得标题,因为默认情况下它是StackNavigator的一部分。

The React Navigation docs also suggests adding a stack navigation for each tab. React Navigation 文档还建议为每个选项卡添加一个堆栈导航。

The bottomTabNavigation screen does not have a header, but a normal StackNavigator does, so you can make your bottom tab open a normal StackNavigator.该bottomTabNavigation屏幕不会有一个头,但一个正常的StackNavigator做,这样就可以使您的底部标签中打开一个正常StackNavigator。

Think of Instagram:想想Instagram:
You open your home tab , and then enter a profile .您打开主页选项卡,然后输入配置文件 When you go back, you still want to be in the home tab.当您返回时,您仍然希望在主页选项卡中。 So it's a Stack Navigation inside a Tab Navigation :)所以它是标签导航中的堆栈导航:)


const HomeStack = createStackNavigator();

function HomeStackScreen() {
  return (
    <HomeStack.Navigator initialRouteName="Feed">
      <HomeStack.Screen name="Feed" component={FeedScreen} />
      <HomeStack.Screen name="Profile" component={ProfileScreen} />
    </HomeStack.Navigator>
  );
}

const Tab = createBottomTabNavigator();

function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator>
        <Tab.Screen name="Home"component={HomeStackScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

Then the StackNavigator screen will add a header based on the name of the screen.然后 StackNavigator 屏幕将根据屏幕名称添加一个标题。 You can also define a custom header title:您还可以定义自定义标题标题:

      <HomeStack.Screen
        name="Home"
        component={HomeScreen}
        options={{ headerTitle: "Custom title" }}
      />
     MyTabs = tabNavigator
     <Stack.Navigator>
     <Stack.Screen name="MyAccount" component={MyTabs} />
    </Stack.Navigator>

1) Use tabNavigator inside stack navigator as it comes with inbuilt header functionality. 1) 在堆栈导航器中使用 tabNavigator,因为它带有内置的标题功能。 2) stack navigator do not have inbuilt header 2)堆栈导航器没有内置标题

it's work for me.这对我有用。 try it as bellow尝试如下

    const TabStack = createBottomTabNavigator({
          Upload: { 
            screen: createStackNavigator({Home: HomeScreen}),,
            navigationOption: {
              headerTitle: "Upload"
            }
          },
          Profile: { 
            screen: profile,
            navigationOption: {`enter code here`
              headerTitle: "Profile"
            }
          }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在底部选项卡导航器 react-navigation 中卸载非活动屏幕? - How to unmount inactive screens in bottom tab navigator react-navigation? 如何在React-Navigation中单击Bottom Tab Navigator打开DrawerNavigator? - How do i open DrawerNavigator upon clicking Bottom Tab Navigator in React-Navigation? 使用时如何从底部标签栏隐藏“SPECIFIC TAB BAR ITEM”:@react-navigation/bottom-tabs - How to hide a "SPECIFIC TAB BAR ITEM" from a bottom tab bar when using: @react-navigation/bottom-tabs 将HOC与react-navigation Navigator一起使用 - Using a HOC with a react-navigation Navigator 如何在反应导航中同时使用抽屉和标签导航器 - How to use both a drawer and a tab navigator in react-navigation 反应导航:切换导航器内的嵌套堆栈导航器和选项卡导航器产生2个标头 - React-navigation: nesting stack navigator and tab navigator inside switch navigator results in 2 headers 反应导航-底部导航 - React-navigation - Bottom Navigation React-Navigation v5 无法导入底部选项卡 - React-Navigation v5 can't import bottom tab React-当我有底部标签导航时如何添加标题 - React - how to add header when I have bottom tab navigation 如何在反应导航的stackNavigator中将标题移到底部? - How to move header to bottom in stackNavigator in react-navigation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM