React navigation tabBarVisible: false not working for StackNavigator inside TabNavigator

本秂侑毒 提交于 2019-12-05 09:11:43

You could use one StackNavigator with all your stacks, and set the TabNavigator as default route :

  • SwitchNavigator
    • Login
    • SignUp
    • StackNavigator
      • TabNavigator
        • FirstTab
        • ListPage
        • Settings
      • EditPage
      • PageTwo

The problem is that your screens (Rewards and AddRewards are inside a Stack Navigator)

OLD:

 Rewards: {
          screen: createStackNavigator({
            Rewards:{
              screen: Rewards,
              navigationOptions:{
                header:null
              }
            },
            AddReward:{
              screen: AddReward,
              navigationOptions:{
                header:null,
                tabBarVisible: false
              }
            }
          })
        }

FIX:

    Rewards: {
          screen: createStackNavigator({
            Rewards,
            AddReward,
          }),
          navigationOptions:{
            header:null,
            tabBarVisible: false
          }
        }

More details are found on the official document, Tested working with v 3.0.9:

A tab navigator contains a stack and you want to hide the tab bar on specific screens

modified code from:

const FeedStack = createStackNavigator({
  FeedHome: FeedScreen,
  Details: DetailsScreen,
});

const TabNavigator = createBottomTabNavigator({
  Feed: FeedStack,
  Profile: ProfileScreen,
});

const AppNavigator = createSwitchNavigator({
  Auth: AuthScreen,
  Home: TabNavigator,
});

To:

const FeedStack = createStackNavigator({
  FeedHome: FeedScreen,
  /* any other route you want to render under the tab bar */
});

const TabNavigator = createBottomTabNavigator({
  Feed: FeedStack,
  Profile: ProfileScreen,
});

const HomeStack = createStackNavigator({
  Tabs: TabNavigator,
  Details: DetailsScreen,
  /* any other route you want to render above the tab bar */
});

const AppNavigator = createSwitchNavigator({
  Auth: AuthScreen,
  Home: HomeStack,
});

Additionally, in this example, FeedScreen will have two headers: one from FeedStack and the other from HomeStack. To solve this problem, one solution is to set default header height of HomeStack to 0 and override height in DetailsScreen. Below is an example:

const HomeStack = createStackNavigator(
  {
    Tabs: TabNavigator,
    Details: DetailsScreen,
    /* any other route you want to render above the tab bar */
  },
  {
    initialRouteName: 'Tabs',
    defaultNavigationOptions: {
      headerStyle: { height: 0, },
      headerForceInset: { top: 'never', bottom: 'never' },
    },
  },
);

The other solution is:

A stack contains a tab navigator and you want to set the title on the stack header

const TabNavigator = createBottomTabNavigator({
  Feed: FeedStack,
  Profile: ProfileStack,
});

TabNavigator.navigationOptions = {
  // Hide the header from AppNavigator stack
  header: null,
};

Well, for me these solutions are kind of tricky. Anyway, it works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!