问题
I have the following stack navigation and screens:
export const HomeStack = createStackNavigator({
Home: HomeScreen,
Categories: CategoriesScreen,
Products: ProductsScreen,
ProductDetails: ProductDetailsScreen,
})
I want to hide tabs only in ProductDetailsScreen
export const hideTabBarComponents = [
'ProductDetails',
]
export const MainTabs = createBottomTabNavigator(
{
Home: HomeStack,
Favorite: FavoriteScreen,
Account: AccountScreen,
Help: HelpScreen,
Events: EventsScreen
},
{
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, tintColor }) => {
...
},
tabBarLabel: ({ focused, tintColor }) => {
...
},
tabBarVisible: ! hideTabBarComponents.includes(navigation.state.routeName)
}),
}
);
The problem that can't pass any options to Tab navigation from Stack Navigation
Not all of the stack screens only one of them
回答1:
After a little search, The following code solved the problem:
HomeStack.navigationOptions = ({ navigation }) => {
let tabBarVisible = true;
let routeName = navigation.state.routes[navigation.state.index].routeName
if ( routeName == 'ProductDetails' ) {
tabBarVisible = false
}
return {
tabBarVisible,
}
}
回答2:
With createBottomTabNavigator you can hide it with the defaultNavigationOptions
defaultNavigationOptions: {
tabBarVisible: false,
},
回答3:
first let's creat a stack navigator and call it StackHome
const StackHome = createStackNavigator(
{
Home: Home,
CustomHide: CustomHide,
});
// This code let you hide the bottom app bar when "CustomHide" is rendering
StackHome.navigationOptions = ({ navigation }) => {
let tabBarVisible;
if (navigation.state.routes.length > 1) {
navigation.state.routes.map(route => {
if (route.routeName === "CustomHide") {
tabBarVisible = false;
} else {
tabBarVisible = true;
}
});
}
return {
tabBarVisible
};
};
export default StackHome;
回答4:
This is how I did. Select the stack in which you want to hide the tab bar. You can select it based on the index.
AppStack.navigationOptions = ({ navigation }) => {
let tabBarVisible = true;
if (navigation.state.index > 0) {
tabBarVisible = false;
}
return {
tabBarVisible
};
};
Here is the link of the docs of React navigation
来源:https://stackoverflow.com/questions/51352081/react-navigation-how-to-hide-tabbar-from-inside-stack-navigation