how to display headers in react navigation with TabNavigation

前端 未结 2 949
野趣味
野趣味 2021-02-06 23:30

I noticed that views in StackNavigation show the header title but if I set those same screens in a TabNavigation it doesn\'t show a header. It only shows a header if I wrap a S

相关标签:
2条回答
  • 2021-02-06 23:45

    Even if this is a fairly old question, I had myself this question a couple days ago, so I'll be adding my two cents about it hoping this will be useful for someone else in the future as well.

    React Navigation is an amazing product with a high amount of customization, but that also turns out sometimes to be confusing to begin with, which also applies to some sections of the documentation. navigationOptions as of the current version states, is common for all screens but the "list of available navigation options depends on the navigator the screen is added to." https://reactnavigation.org/docs/tab-navigator.html#navigationoptions-used-by-tabnavigator hence the header option doesn't work because it's not available for TabNavigator itself.

    Regarding your question on which approach is the best that depends on what do you want to accomplish with the navigation for your app itself. If you put your TabNavigator inside a StackNavigator the header component will be common for all of the tabs inside the TabNavigator, meaning the tab transition will take effect but the header won't move from its top position. If you on the other hand choose to nest a StackNavigator inside every tab, the header will render inside every tab, meaning the header will move along the tab transition animation.

    I made a quick demo for you to see the difference, the code is also available if you wanna play with it.

    0 讨论(0)
  • 2021-02-07 00:00

    According to React-Navigation TabNavigator Docs there is no header navigationOption. Therefore, when you write the following code you are actually setting a nonexisting value thus what you are doing does not affect anything.

    navigationOptions: {
      header: { visible: true },
    }
    

    Sadly, you need a StackNavigator in this situation.

    0 讨论(0)
提交回复
热议问题