how-to-hide-uitabbarcontroller)

來源:互聯網
上載者:User

http://stackoverflow.com/questions/5272290/how-to-hide-uitabbarcontroller

I am pasting this from my working code... you can call these methods to hide and show the tabbarcontroller.... just pass tabbarcontroller instance to these functions..

 

1 // Method call2 [self hideTabBar:self.tabBarController];   
// 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]; }

 

Modified Setomidor's answer to work on both landscape, portrait, and iPad (the 320 and 480 values only work on 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 - 49.0;    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )    {        fHeight = screenRect.size.width - 49.0;    }    [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]; }

Saurabh's answer above can be extended to also work in landscape orientation:

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

 

Saurahb and karlbecker_com's solutions are great, though they can cause an obvious popping effect when the view contains a tableview while the tab bar animates back up. I've made some modifications and combined it into a single function (as a category on UITabBarController). It's not completely perfect (delayed correction animation) but gives good results with tables.

If you like animation blocks and categories, give this a try. Orientation and device friendly.

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.