React navigation tabBarVisible: false not working for StackNavigator inside TabNavigator

余生长醉 提交于 2019-12-22 05:58:17

问题


I am totally confused to achieve something like below, this is working fine but can't hide tab bar for EditPage and PageTwo

  • Login
  • SignUp
  • TabNavigator(TabNavigator)
    • FirstTab
    • SecondTab(StackNavigator)
      • ListPage
      • EditPage
    • ThirdTab(StackNavigator)
      • PageOne
      • PageTwo
    • Settings

Below is my configuration for it.

import { createStackNavigator, createSwitchNavigator, createBottomTabNavigator } from 'react-navigation';
//Other required imports here

const SignedOut = createStackNavigator({
  Signup: { screen : Signup},
  Login: { screen : Login}
});

const SignedIn = createBottomTabNavigator({
    Dashboard: {
      screen: Dashboard
    },
    Rewards: {
      screen: createStackNavigator({
        Rewards:{
          screen: Rewards,
          navigationOptions:{
            header:null
          }
        },
        AddReward:{
          screen: AddReward,
          navigationOptions:{
            header:null,
            tabBarVisible: false
          }
        }
      })
    },
    Activities: {
      screen: createStackNavigator({
        Rewards:{
          screen: Activities,
          navigationOptions:{
            header:null
          }
        },
        NewActivity:{
          screen: NewActivity,
          navigationOptions:{
            header:null,
            tabBarVisible: false
          }
        }
      })
    },
    Settings: {
      screen: Settings
    }
},{
  tabBarComponent: ({navigation}) => <FooterComponent navigation={navigation} />,
  tabBarPosition: 'bottom',
  animationEnabled: false,
  swipeEnabled: false
});

export const createRootNavigator = (signedIn) => {
    return createSwitchNavigator(
        {
            SignedIn: {
                screen: SignedIn
            },
            SignedOut: {
                screen: SignedOut
            }
        },
        {
            initialRouteName: (signedIn) ? "SignedIn" :"SignedOut",
            headerMode: 'none'
        }
    );
};

Problems

  • I can't hide tab bar for AddReward and NewActivity
  • tabBarVisible: false not working for StackNavigator inside TabNavigator

Thanks


回答1:


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



回答2:


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
          }
        }



回答3:


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.



来源:https://stackoverflow.com/questions/50701545/react-navigation-tabbarvisible-false-not-working-for-stacknavigator-inside-tabn

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