iGatiTech
iGatiTech

Reputation: 2268

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?

Upvotes: 0

Views: 587

Answers (3)

Luan Tran
Luan Tran

Reputation: 1142

Let try to hide it when view will disappear

-(void) viewWillDisappear:(BOOL)animated{
     [self setTabBarHidden:YES];
     [super viewWillDisappear:animated];
}

and show it when view will appear

-(void) viewWillAppear:(BOOL)animated{
     [self setTabBarHidden:NO];
     [super viewWillAppear];
}

Upvotes: 0

arturdev
arturdev

Reputation: 11039

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];

Upvotes: 1

Jagdeep
Jagdeep

Reputation: 1178

Trying checking Hide Bottom Bar on Push for your ViewController to be opened on storyboard enter image description here

or you can also use it as this in programmatically approach

      self.hidesBottomBarWhenPushed = true
      let destinationVC = self.storyboard?.instantiateViewControllerWithIdentifier("StoryBoardID") as! yourVC
      self.navigationController?.pushViewController(destinationVC, animated: true)
      self.hidesBottomBarWhenPushed = false

Upvotes: 0

Related Questions