xcode 需要时从视图控制器显示/隐藏标签栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9159380/
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
show/hide the tabbar when needed from a view controller
提问by Anshuk Garg
i am new to iOS programming. i really need your help.
我是 iOS 编程的新手。我真的需要你的帮助。
i have a login screen that takes me to a map (google API). on clicking any created annotation i want to load a tabbar with 2 views.
我有一个登录屏幕,可以将我带到地图(谷歌 API)。单击任何创建的注释时,我想加载一个带有 2 个视图的标签栏。
i searched and found out that i need to add the tabbar at the starting ie the appdelegate and show/hide the tabbar when needed.
我搜索并发现我需要在开始时添加标签栏,即 appdelegate 并在需要时显示/隐藏标签栏。
so i made 2 functions to show and hide tabbar as
所以我做了 2 个函数来显示和隐藏标签栏
-(void)Load_tabBar{
[self.navigationController.view removeFromSuperview];
[self.window addSubview:tabBarController.view];
[self.window makeKeyWindow];}
-(void)remove_tabBar{
self.tabBarController.selectedIndex=0;
[self.tabBarController.view removeFromSuperview];
[self.window addSubview:navigationController.view];
[self.window makeKeyWindow];}
it did work when i call the Load_tabBar method and when i click back it calls remove_tabBar method. if i again call Load_tabBar method and back, it crashes giving error
当我调用 Load_tabBar 方法时它确实有效,当我单击返回时它调用 remove_tabBar 方法。如果我再次调用 Load_tabBar 方法并返回,它会崩溃并给出错误
-[UILayoutContainerView window]: message sent to deallocated instance 0x563b0b0
-[UILayoutContainerView 窗口]:消息发送到释放实例 0x563b0b0
edited: PS : can i add tabbar view to a view controller and then push that view?
编辑: PS:我可以将标签栏视图添加到视图控制器,然后推送该视图吗?
thnx
谢谢
回答by Tendulkar
use this self.hidesBottomBarWhenPushed = YES;
用这个 self.hidesBottomBarWhenPushed = YES;
回答by horacex
This method definitely works. You just need to put it in the method BEFORE you push it, like this:
这个方法绝对有效。你只需要在你推送它之前把它放在方法中,就像这样:
-actionThatPushTheViewController {
//create the view controller here, do some init.
//then:
theViewControllerToBePushed.hidesBottomBarWhenPushed = YES;
//push it here like this:
[self.navigationController pushViewController:theViewControllerToBePushed animated:YES];
回答by Nilesh Kikani
I hope this two methods may help you,
希望这两种方法可以帮到你
- (void) hideTabBar:(UITabBarController *) tabbarcontroller {
int height = 480;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
for(UIView *view in tabbarcontroller.view.subviews) {
if([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
}
else {
[view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y, 320, 436)];
}
}
[UIView commitAnimations];
}
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller {
int height = 480;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
for(UIView *view in tabbarcontroller.view.subviews) {
if([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
}
else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, height)];
}
}
[UIView commitAnimations];
}
}
Just put this two methods in AppDelegate class and call it where ever required as per you requirement.
只需将这两个方法放在 AppDelegate 类中,并根据您的要求在需要的地方调用它。
回答by Govani Dhruv Vijaykumar
if you want to hide it when pushed and show it when popped here is code:
如果你想在推送时隐藏它并在弹出时显示它,这里是代码:
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondVC") as? SecondVC {
if let navigator = navigationController {
viewController.hidesBottomBarWhenPushed = true
navigator.pushViewController(viewController, animated: true)
}
}

