Hide the button of the central tab bar while the HidesBottomBarWhenPushed view pane is pressed

I follow the example of creating a tab bar with a central button like Path, Instagram, etc. here: http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

The problem is that when the 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 ran into this problem, but there is no working solution. (I tried all the suggested solutions in the comments)

I came up with a hacker solution - I saved the link to the central button in an unrelated singleton class, and then clicked on the view that hides the button when it loads, and displays it when it disappears ... but it just feels wrong and looks funny. because you can see that the button disappears before the start of the push view animation.

Has anyone got this job?

+7
source share
3 answers

I had the same problem. I edited BaseViewController.m (my subclass of UITabBarController), overriding the following viewDidLayoutSubviews method (the button is my center button) as shown below.

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

Your button now follows the tab.

+6
source

You should do this, but with a UIImageView and add it to the 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; } 
+2
source

Before clicking on the UIViewController add your custom button to the UITabBar

After pop UIViewController restore custom button for self.view

Subclass of UITabViewController

 NSArray *array= self.viewControllers; for(UIViewController *controller in array){ if([controller isKindOfClass:[UINavigationController class]]){ UINavigationController *navigationController=(UINavigationController*)controller; navigationController.delegate=self; } } 

Implement delegation method

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController.hidesBottomBarWhenPushed) { CGRect rect= [button convertRect:button.bounds toView:self.tabBar]; [button removeFromSuperview]; [self.tabBar addSubview:button]; button.frame=rect; } } -(void)navigationController:(nonnull UINavigationController *)navigationController didShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated{ if(!viewController.hidesBottomBarWhenPushed){ CGRect rect= [button convertRect:button.bounds toView:self.view]; [button removeFromSuperview]; [self.view addSubview:button]; button.frame=rect; } } 
+1
source

All Articles