从视图控制器显示/隐藏标签栏

时间:2012-02-06 11:24:34

标签: ios xcode ipad

我是iOS编程新手。我真的需要你的帮助。

我有一个登录屏幕,可以将我带到地图(谷歌API)。在单击任何创建的注释时,我想加载一个包含2个视图的标签栏。

我搜索并发现我需要在开始时添加标签栏,即appdelegate,并在需要时显示/隐藏标签栏。

所以我做了2个函数来显示和隐藏tabbar为

-(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];}

当我调用Load_tabBar方法时它确实有效,当我点击它时调用remove_tabBar方法。如果我再次调用Load_tabBar方法并返回,它会崩溃给出错误

- [UILayoutContainerView窗口]:发送到解除分配的实例0x563b0b0的消息

编辑:PS:我可以将标签栏视图添加到视图控制器,然后推送该视图吗?

日Thnx

4 个答案:

答案 0 :(得分:10)

使用此self.hidesBottomBarWhenPushed = YES;

答案 1 :(得分:1)

我希望这两种方法可以帮到你,

- (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];

}

将这两个方法放在AppDelegate类中,并根据您的要求在需要的地方调用它。

答案 2 :(得分:1)

这种方法绝对有效。您只需要在推送之前将其放入方法中,如下所示:

-actionThatPushTheViewController { 

    //create the view controller here, do some init. 

    //then:
    theViewControllerToBePushed.hidesBottomBarWhenPushed = YES;

    //push it here like this: 
    [self.navigationController pushViewController:theViewControllerToBePushed animated:YES];

答案 3 :(得分:0)

如果要在按下时将其隐藏并在弹出时显示,请输入代码:

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