如何在 iOS 中隐藏/显示带有导航栏的视图的选项卡栏?
我有带有导航栏和选项卡栏的视图。我想要发生的事情是隐藏某个视图上的选项卡栏,并在用户更改视图时再次显示选项卡栏。
我看到了一段隐藏标签栏的代码:
-(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;
}
来自: http ://nickwaynik.com/iphone/hide-tabbar-in-an-ios-app/
我在视图上调用它,其中我希望隐藏选项卡栏,
[self makeTabBarHidden:YES];
当我在该视图上显示/隐藏它时它工作正常但是当我导航回到上一个视图中,标签栏也被隐藏了。我尝试在视图的 viewDidUnload
、viewWillDisappear
、viewDidDisappear
函数中调用该函数,但没有任何反应。在上一个视图的 viewDidLoad
、viewWillAppear
、viewDidAppear
函数中调用该函数时也是如此。
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/
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以设置 UIViewController.hidesBottomBarWhenPushed 来代替:
You can set the UIViewController.hidesBottomBarWhenPushed instead:
您还可以在故事板的 Interface Builder 中执行此操作。选择要隐藏选项卡栏的视图控制器,然后选择“按下时隐藏底部栏”。
You can also do this in the Interface Builder for a storyboard. Select the View Controller that you want to hide the Tab Bar for and then select "Hide Bottom Bar on Push".
我刚刚在 UITabBarController 上创建了一个类别,允许您隐藏 TabBar,可以选择使用动画:
https://github.com/idevsoftware/Cocoa-Touch-Additions/tree/master/UITabBarController_setHidden
它添加了
tabBarHidden
属性(以isTabBarHidden
作为其 getter)和- (void)setTabBarHidden:(BOOL)hiddenanimated:(BOOL)animated
方法。I just created a category on UITabBarController that allows you to hide the TabBar, optionally with an animation:
https://github.com/idevsoftware/Cocoa-Touch-Additions/tree/master/UITabBarController_setHidden
It adds the
tabBarHidden
property (withisTabBarHidden
as its getter) and the- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated
method.斯威夫特3:
在
viewWillAppear
或viewDidAppear
中设置标签栏隐藏Swift 3:
Set tab bar to hide in
viewWillAppear
orviewDidAppear
尝试隐藏/显示:
Try this for hide / show:
将此行添加到您的
viewDidLoad
或viewWillAppear
中;这将从底部隐藏您的选项卡。Add this line to your
viewDidLoad
orviewWillAppear
; this will hide you tab from bottom.当您单击 Xib 或故事板文件上的视图控制器时,属性检查器上也提供相同的属性。
The same property is available on the attributes inspector when you click on your view controller on your Xib or storyboard file.
您可以使用下面的代码,但当您向后导航时,
tabBar
仍然隐藏。更好的方法是通过 main.storyboard 来完成
像我一样检查“按下时隐藏底部栏”。
you can use below code but
tabBar
remains hidden when you navigate back.better way is to do through main.storyboard
check
"Hide Bottom Bar on Push"
as I've done.