开发者

How to hide UITabBar on a UITabBarController that I didn't subclass?

开发者 https://www.devze.com 2023-04-09 09:30 出处:网络
Visually this is what I want I have a UITabBarController. I want to hide the UITabBar when the user enters the middle tab. The middle tab\'s loads a view controller of class B. This is the behavior o

Visually this is what I want

I have a UITabBarController. I want to hide the UITabBar when the user enters the middle tab. The middle tab's loads a view controller of class B. This is the behavior of the popular camera app Instagram. Their middle tab loads up a full screen camera.

-------------    -------------    -------------
|     VC    |    |     VC    |    |     VC    |
|    for    |    |    for    |    |    for    |
|     A     |    |     B     |    |     C     |
|           |    |           |    |           |
|------------    |    开发者_高级运维       |    |------------
{ A } B | C |    |           |    | A | B { C }
-------------    -------------    -------------

Proposed solution from all other related StackExchange questions

We already have dozens of questions on how to hide the UITabBar when a particular view controller is pushed. The general consensus is this:

b.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b
                                     animated:YES];

My problem is, I never subclassed the UITabBarController. I created it in Interface Builder. I never manually push my view controllers, so the above solution doesn't work for me.

Failure attempt 1

Inside my middle view controller, I turn on hidesBottomBarWhenPushedin the constructor. This had no effect.

@implementation B

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

Failure attempt 2

I also tried assigning my app delegate as a UITabBarControllerDelegate. When the UITabBarController notifies me that a tab has been tapped, I turn on hidesBottomBarWhenPushed only for the middle view controller. This also failed to hide the UITabBar.

#pragma mark UIApplicationDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [window addSubview:self.rootViewController.view];
    [window makeKeyAndVisible];
    self.rootViewController.delegate = self;
}

#pragma mark UITabBarControllerDelegate

- (void) tabBarController:(UITabBarController *)tabBarController 
didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[B class]]) {
        viewController.hidesBottomBarWhenPushed = YES;
    } else {
        viewController.hidesBottomBarWhenPushed = NO;
    }
}


- (void) hidetabbar:(BOOL)hiddenTabBar
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    for(UIView *view in self.uiTabBarController.view.subviews){
        if([view isKindOfClass:[UITabBar class]]) {

            if (hiddenTabBar) {
                [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            }
        } else {
            if (hiddenTabBar) {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            } else {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            }
        }
    }
    [UIView commitAnimations];  
    hiddenTabBar = !hiddenTabBar;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号