Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide center tab bar button when pushed view sets HidesBottomBarWhenPushed

I'm following the example on how to create a tab bar with a center button like Path,Instagram, etc from here: http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

The problem I have is that when a view is pushed onto the stack that sets HidesBottomBarWhenPushed to hide the tab bar, the center button is still displayed.

In the comments, several other people have had this problem, but there's no working solution. (I've tried all the suggested solutions in the comments)

I came up with a hacky solution-- store a reference to the center button in an unrelated singleton class, and then have the pushed view hide the button when it is loaded, and unhide it when it dissapears-- but this just feels wrong, and it looks funny because you can see the button dissapear before the push view animation starts.

Has anyone got this working?

like image 270
ch3rryc0ke Avatar asked Jan 07 '12 09:01

ch3rryc0ke


2 Answers

I had the same problem. I edited the BaseViewController.m (my UITabBarController subclass) by overriding the following viewDidLayoutSubviews method (button is my center button) as followed.

- (void)viewDidLayoutSubviews{
    button.center = self.tabBar.center;
}

Now your button follows the tabbar.

like image 195
Matthieu Rouif Avatar answered Oct 11 '22 00:10

Matthieu Rouif


You have to do this same but with UIImageView and add it to tabBar:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:               (UIViewController *)viewController
{
    if (tabBarController.selectedIndex != AUCenterTabBarButtonIntex) {
        self.centerImageView.highlighted = NO;
    } else {
        self.centerImageView.highlighted = YES;
        self.selectedIndex = AUCenterTabBarButtonIntex;
    }

}


- (void)addCenterImageViewWithImage:(UIImage *)image highlitedImage:(UIImage *)highlitedImage
{
    UIImageView *centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width/2, image.size.height/2)];
    centerImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;

    centerImageView.image = image;
    centerImageView.highlightedImage = highlitedImage;

    CGFloat heightDifference = centerImageView.frame.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        centerImageView.center = CGPointMake(self.tabBar.center.x, centerImageView.center.y);
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = (self.tabBar.frame.size.height/2) - (heightDifference/2);
        centerImageView.center = center;
    }

    [self.tabBar addSubview:centerImageView];

    self.centerImageView = centerImageView;
}
like image 42
Michal Zaborowski Avatar answered Oct 11 '22 02:10

Michal Zaborowski