Search code examples
iosobjective-cuitabbarcontrolleruitabbar

UITabBarController Custom Button not hiding while moving to next ViewController


I have added custom center button in UITabBarViewController, code as below...

self.centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.centerButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
self.centerButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[self.centerButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.centerButton setBackgroundImage:highlightImage forState:UIControlStateHighlighted];

CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
  if (heightDifference < 0) {
        self.centerButton.center = self.tabBar.center;
  } else {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        self.centerButton.center = center;
  }

[self.centerButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.centerButton];

Please check attached screenshot for reference,

enter image description here

Now when I am trying to hide it while moving to other viewcontroller, only tabbar gets hidden and not the center custom button with below code,

[self.tabBarController.tabBar setHidden:YES];

Also tried like this to hide center custom button,

 WBTabBarController *objWBTab = [self.storyboard instantiateViewControllerWithIdentifier:ID_CNTRL_TABBAR];
 [objWBTab setTabBarHidden:YES];

SetTabBarHidden has been defined as below in WBTabBarController.m,

- (void)setTabBarHidden:(BOOL)tabBarHidden
{
    self.centerButton.hidden = tabBarHidden;
    self.tabBar.hidden = tabBarHidden;
}

Still no luck, can anybody help me please?


Solution

  • Its because you're adding your button as subview to your entire view instead of tabBar's view.

    Change [self.view addSubview:self.centerButton]; line with
    [self.tabBar addSubview:self.centerButton];