如何隐藏uitabbarcontroller

Vik*_*yan 75 iphone uitabbarcontroller ios

我有问题UITabBarController.在我的应用程序中,我想隐藏它但没有使用,hidesBottomBarWhenPushed因为我想隐藏它而不是当我推它时.例如,我想在我的应用程序中按"隐藏"按钮时隐藏它.

我在谷歌阅读了很多文章,但我无法知道如何做到这一点.

Sau*_*abh 149

我从我的工作代码中粘贴这个...你可以调用这些方法来隐藏和显示tabbarcontroller ....只需将tabbarcontroller实例传递给这些函数..

// Method call
[self hideTabBar:self.tabBarController];   
Run Code Online (Sandbox Code Playgroud)
// Method implementations
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];

    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
        }
    }

    [UIView commitAnimations];   
}

- (void)showTabBar:(UITabBarController *) tabbarcontroller
{       
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        NSLog(@"%@", view);

        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];

        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
        }
    }

    [UIView commitAnimations]; 
}
Run Code Online (Sandbox Code Playgroud)

  • 我正在尝试这个解决方案.当调用hideTabbar()方法时,我的标签栏是隐藏的,但它在底部显示黑色空格(位置相同的标签栏).我怎么能解决这个问题? (3认同)

kar*_*com 58

改进的Setomidor对横向,纵向和iPad工作的回答(320和480值仅适用于iPhone).

- (void) hideTabBar:(UITabBarController *) tabbarcontroller 
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width;
    }

    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
            view.backgroundColor = [UIColor blackColor];
        }
    }
    [UIView commitAnimations];
}



- (void) showTabBar:(UITabBarController *) tabbarcontroller 
{   
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float fHeight = screenRect.size.height - tabbarcontroller.tabBar.frame.size.height;

    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width - tabbarcontroller.tabBar.frame.size.height;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in tabbarcontroller.view.subviews)
    {   
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];            
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
        }       
    }
    [UIView commitAnimations]; 
}
Run Code Online (Sandbox Code Playgroud)

还修改了代码以处理iOS 6中引入的更改,并更改了UIDevice方向,并确保即使设备背面也能正常工作.

  • 您应该将` - 49.0`替换为`tabbarcontroller.tabBar.frame.size.height`以获得更清晰的代码,这些代码在未来的iOS版本中具有更少的破解机会. (10认同)

Sai*_*esh 34

在按钮的操作方法中:

[self.tabBarController.tabBar setHidden:YES];
Run Code Online (Sandbox Code Playgroud)

  • 此方法适用于iOS 7,但在iOS 6中,它会在标签栏中留下很大的差距. (2认同)

Tho*_*eek 12

Saurahb和karlbecker_com的解决方案很棒,但是当视图包含桌面视图时,它们会导致明显的弹出效果,同时标签栏会动画回来.我做了一些修改并将它组合成一个函数(作为UITabBarController上的一个类别).它不是完全完美的(延迟校正动画),但在表格上给出了很好的结果.

如果您喜欢动画块和类别,请尝试一下.方向和设备友好.

的UITabBarController + ShowHideBar.m:

#import "UITabBarController+ShowHideBar.h"

@implementation UITabBarController (ShowHideBar)

- (void) setHidden:(BOOL)hidden{

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ){
        fHeight = screenRect.size.width;
    }

    if(!hidden) fHeight -= self.tabBar.frame.size.height;

    [UIView animateWithDuration:0.25 animations:^{
        for(UIView *view in self.view.subviews){
            if([view isKindOfClass:[UITabBar class]]){
                [view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
            }else{
                if(hidden) [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
            }
        }
    }completion:^(BOOL finished){
        if(!hidden){

            [UIView animateWithDuration:0.25 animations:^{

                for(UIView *view in self.view.subviews)
                {
                    if(![view isKindOfClass:[UITabBar class]])
                        [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
                }

            }];
        }
    }];

}

@end
Run Code Online (Sandbox Code Playgroud)

的UITabBarController + ShowHideBar.h:

#import <UIKit/UIKit.h>

@interface UITabBarController (ShowHideBar)

- (void) setHidden:(BOOL)hidden;

@end
Run Code Online (Sandbox Code Playgroud)

用法:

[self.tabBarController setHidden:YES];
[self.tabBarController setHidden:NO];
Run Code Online (Sandbox Code Playgroud)


Set*_*dor 9

Saurabh上面的答案可以扩展到横向工作:

+ (void) hideTabBar:(UITabBarController *) tabbarcontroller {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];

    //Support for landscape views
    int orientation = [[UIDevice currentDevice] orientation];
    int x_pos = 480;
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        x_pos = 320;
    }

    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, x_pos, view.frame.size.width, view.frame.size.height)];
        } 
        else 
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, x_pos)];
        }       
    }   
    [UIView commitAnimations]; 
}
Run Code Online (Sandbox Code Playgroud)

`

showTabBar()的相应x_pos数字是431271.