如何在 iOS 中使用导航栏隐藏/显示视图的标签栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6383069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide/show tab bar of a view with a navigation bar in iOS?
提问by dork
I have views with a navigation bar and a tab bar. What I would like to happen is to hide the tab bar on a certain view and show the tab bar again when the user changes views.
我有一个导航栏和一个标签栏的视图。我想要发生的是隐藏某个视图上的标签栏,并在用户更改视图时再次显示标签栏。
I saw a snippet of code for hiding the tab bar:
我看到了一段隐藏标签栏的代码:
-(void)makeTabBarHidden:(BOOL)hide
{
// Custom code to hide TabBar
if ( [tabBarController.view.subviews count] < 2 ) {
return;
}
UIView *contentView;
if ( [[tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ) {
contentView = [tabBarController.view.subviews objectAtIndex:1];
} else {
contentView = [tabBarController.view.subviews objectAtIndex:0];
}
if (hide) {
contentView.frame = tabBarController.view.bounds;
}
else {
contentView.frame = CGRectMake(tabBarController.view.bounds.origin.x,
tabBarController.view.bounds.origin.y,
tabBarController.view.bounds.size.width,
tabBarController.view.bounds.size.height - tabBarController.tabBar.frame.size.height);
}
tabBarController.tabBar.hidden = hide;
}
from: http://nickwaynik.com/iphone/hide-tabbar-in-an-ios-app/
来自:http: //nickwaynik.com/iphone/hide-tabbar-in-an-ios-app/
I call this on the view wherein I want the tab bar hidden
我在想要隐藏标签栏的视图上调用它
[self makeTabBarHidden:YES];
it works fine when i show/hide it on that view but when I navigate back to the previous view, the tab bar there is also hidden. I tried calling that function in the view's viewDidUnload
, viewWillDisappear
, viewDidDisappear
functions but nothing happens. The same is true when the function is called in the previous view's viewDidLoad
, viewWillAppear
, viewDidAppear
functions.
当我在该视图上显示/隐藏它时它工作正常,但是当我导航回上一个视图时,标签栏也被隐藏了。我尝试在视图的viewDidUnload
, viewWillDisappear
,viewDidDisappear
函数中调用该函数,但没有任何反应。在前面视图的viewDidLoad
, viewWillAppear
,viewDidAppear
函数中调用该函数时也是如此。
回答by ushika
You can set the UIViewController.hidesBottomBarWhenPushed instead:
你可以设置 UIViewController.hidesBottomBarWhenPushed 代替:
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.hidesBottomBarWhenPushed = YES;
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
回答by Suragch
回答by boliva
I just created a category on UITabBarController that allows you to hide the TabBar, optionally with an animation:
我刚刚在 UITabBarController 上创建了一个类别,允许您隐藏 TabBar,可选择使用动画:
https://github.com/idevsoftware/Cocoa-Touch-Additions/tree/master/UITabBarController_setHidden
https://github.com/idevsoftware/Cocoa-Touch-Additions/tree/master/UITabBarController_setHidden
It adds the tabBarHidden
property (with isTabBarHidden
as its getter) and the - (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
method.
它添加了tabBarHidden
属性(isTabBarHidden
作为它的 getter)和- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
方法。
回答by Yogesh Dalavi
self.navigationController.hidesBottomBarWhenPushed=YES;
Add this line to your viewDidLoad
or viewWillAppear
; this will hide you tab from bottom.
将此行添加到您的viewDidLoad
或viewWillAppear
;这将从底部隐藏你的标签。
回答by alicanozkara
Try this for hide / show:
试试这个隐藏/显示:
- (void)viewWillDisappear:(BOOL)animated {
self.hidesBottomBarWhenPushed = NO;
}
- (void)viewWillAppear:(BOOL)animated {
self.hidesBottomBarWhenPushed = YES;
}
回答by Sandu
Swift 3:
Set tab bar to hide in viewWillAppear
or viewDidAppear
Swift 3:将标签栏设置为隐藏viewWillAppear
或viewDidAppear
self.tabBarController?.tabBar.isHidden = true
回答by ShowPony
The same property is available on the attributes inspector when you click on your view controller on your Xib or storyboard file.
当您单击 Xib 或故事板文件上的视图控制器时,属性检查器上可以使用相同的属性。
回答by Soropromo
you can use below code but tabBar
remains hidden when you navigate back.
您可以使用以下代码,但tabBar
在您返回时保持隐藏状态。
//hide tabbar
//self.tabBarController?.tabBar.isHidden = true
better wayis to do through main.storyboard
check "Hide Bottom Bar on Push"
as I've done.
更好的方法是"Hide Bottom Bar on Push"
像我一样通过 main.storyboard 检查。