简体   繁体   English

React Native和React Navigation - 如何在标题和底部标签导航器中显示屏幕标题以显示

[英]React Native and React Navigation — How to get screen title to show in header and bottom tab navigator to also show

I am teaching myself React Native and building a fairly simple app, irMobile . 我正在教自己React Native并构建一个相当简单的应用程序, irMobile

I've set up each screen with (using the About screen as an example) 我已经设置了每个屏幕(以“关于”屏幕为例)

static navigationOptions = () => ({
  title: 'About'
})

and in my app's main component 并在我的应用程序的主要组件中

const MainNavigator = createBottomTabNavigator({ About })
const StackNavigator = createStackNavigator({ MainNavigator })
const AppContainer = createAppContainer(StackNavigator)

then in the component itself 然后在组件本身

  render() {
    return (
      <View style={styles.container}>
        <AppContainer />
      </View>
    )
  }

My screens render with a nice nav bar at the bottom but I don't get any titles in the header, just an empty space. 我的屏幕渲染底部有一个漂亮的导航栏,但我没有在标题中获得任何标题,只是一个空白空间。

I've gone through the docs several times looking to see what I have missed and I'm stumped. 我已经多次浏览文档 ,看看我错过了什么,我很难过。

If I use the inspector in the iOS simulator I can select the header and it shows it to be a View at x: 0, y: 44 with width: 375 and height: 43.7 , so the thing is there I just can't see it. 如果我在iOS模拟器中使用检查器,我可以选择标题,并将其显示为x: 0, y: 44处的Viewwidth: 375height: 43.7 ,所以事情就在那里我看不到它。

I've tried setting a headerStyle to { backgroundColor: 'red' } to see if I can at least see that, but no. 我已经尝试将headerStyle设置为{ backgroundColor: 'red' }以查看我是否至少可以看到它,但是没有。

屏幕快照的iPhone模拟器显示标题

However, if I change the StackNavigator to put the About screen in there along with the MainNavigator as follows: 但是,如果我改变StackNavigatorAbout屏幕在那里与一起MainNavigator如下:

const StackNavigator = createStackNavigator({ About, MainNavigator })

Then I get my header showing up as expected, but the bottom tab bar no longer shows. 然后我按照预期显示我的标题,但底部的标签栏不再显示。

屏幕显示iPhone显示标题但缺少标签导航器

So that's confusing. 这令人困惑。

The docs are a bit vague on how to get both a header and a bottom tab navigator into the same app. 关于如何将标题和底部标签导航器同时放入同一个应用程序,文档有点模糊。 Clearly, being a bit of a React Native noob here, I am missing something really obvious and simple. 显然,在这里成为一个React Native noob,我遗漏了一些非常明显和简单的东西。

What am I failing to understand and how do I fix this? 我没有理解什么,我该如何解决这个问题?

Source code at github.com/davesag/irMobile . 源代码在github.com/davesag/irMobile

By setting navigationOptions inside the component, you apply it to the navigation element that is calling the component. 通过在组件内设置navigationOptions ,可以将其应用于调用组件的导航元素。

So in your case, the title you define in About applies to the bottom tab, and not to the screen as you would like to. 因此,在您的情况下,您在“ About定义的标题将应用于底部选项卡,而不是您希望的屏幕。

Rather than setting navigationOptions as a static property inside the component, you need to define it directly when creating the stack navigation. 不是将navigationOptions设置为组件内的静态属性,而是在创建堆栈导航时直接定义它。

In addition to this, I think you want to nest the stack inside the bottom tab navigation, not the other way around. 除此之外,我认为您希望将堆栈嵌套在底部选项卡导航中,而不是相反。 By doing so, you can have a different title for each screen. 通过这样做,您可以为每个屏幕设置不同的标题。

Here is an example: 这是一个例子:

const AboutStack = createStackNavigator({ About: { screen: About, navigationOptions: { title: 'About Title' } } });
const CreditStack = createStackNavigator({ Credit: { screen: Credit, navigationOptions: { title: 'Credit Title' } } });

const MainNavigator = createBottomTabNavigator({
  About: { screen: AboutStack, navigationOptions: { title: 'About Label' } },
  Credit: { screen: CreditStack, navigationOptions: { title: 'Credit Label' } },
});

const AppContainer = createAppContainer(MainNavigator);

Like this, the header title will be "About Title" and the tab label "About Label". 像这样,标题标题将是“关于标题”和标签标签“关于标签”。

Okay I have worked it out. 好的,我已经解决了。

The trick is to use getActiveChildNavigationOptions . 诀窍是使用getActiveChildNavigationOptions

So now my code looks like 所以现在我的代码看起来像

const navBar = useStorybook({ Balances, Settings, About })
const MainNavigator = createBottomTabNavigator(navBar, {
  navigationOptions: ({ navigation, screenProps }) =>
    getActiveChildNavigationOptions(navigation, screenProps)
})
const StackNavigator = createStackNavigator({ MainNavigator })
const AppContainer = createAppContainer(StackNavigator)

As @Remeus pointed out, the titles in my actual screen components were only making it to the MainNavigator and not accessible by the StackNavigator . 作为@Remeus指出,在我的实际屏幕组件的标题只能够进入MainNavigator ,而不是由可访问StackNavigator

Rather than invert my stack however and create a StackNavigator for each tab, it seems to me to be cleaner to leverage getActiveChildNavigationOptions to grab the screen components' title (and other) navigationOptions . 而不是反转我的堆栈并为每个选项卡创建一个StackNavigator,在我看来,利用getActiveChildNavigationOptions来获取屏幕组件的title (和其他) navigationOptions更清晰。

This works perfectly. 这非常有效。

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

相关问题 如何将标题 / header 标题添加到我们通过创建底部选项卡导航器(反应导航 v5)导航的每个屏幕? - How to add title / header title to each screen which we navigate through create bottom tab navigator ( react navigation v5)? React Native 如何在底部选项卡导航器上方显示元素 - React Native How to show an element above bottom tab navigator 如何使用 React Navigation 在 React Native 中显示标题标题 - How to show header title in React Native using React Navigation 在 React Native 中从底部选项卡导航器的标题导航 - Navigation from the header of the bottom tab navigator in react native React-navigation:使用底部选项卡导航器时不显示标题 - React-navigation: header does not show up when using bottom tab navigator 嵌套选项卡导航器中的 React Navigation header 标题 - React Navigation header title in nested tab navigator 如何在 React Native 中将反应导航堆栈 header 移动到屏幕底部? - How to move react navigation Stack header to bottom of the screen in React native? 无法在特定屏幕上的 React Native 中隐藏或显示选项卡底栏 - Not able to hide or show tab bottom bar in React Native on particular screen 反应本机选项卡导航屏幕标题未显示 - React native tab navigation screen title not showing 如何在react-native中使用navigator组件显示导航栏 - how to show navigation bar using navigator component in react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM