Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add subview over UITabBar showing under Tab Bar when vc popped

I have a simple Tab Bar project where I add an image over the tab bar which looks good at first.

A second view is pushed form FirstViewController with hidesBottomBarWhenPushed = true. I then hide the button which was added, for good reasons, on the view and not the tabbar as a subview.

Everything ok until I press back button and when I unhide the view (or recreate it) show under the tab bar.

In what method should I put the showButton() method so it shows over the tabbar again?

Any tips?

CustomTabBarController.swift

class CustomTabBarController: UITabBarController {

    var bigButton: UIImageView!

    func addCenterButton() {
      let image = UIImage(named: "bigButtonBackground")
      bigButton = UIImageView(image: image)
      bigButton.frame = CGRectMake(0.0, 0.0, image!.size.width, image!.size.height);
      bigButton.autoresizingMask = .FlexibleRightMargin | .FlexibleLeftMargin | .FlexibleBottomMargin | .FlexibleTopMargin
      let heightDifference = image!.size.height - tabBar.frame.size.height
      var center = tabBar.center
      center.y = center.y - heightDifference/2.0
      bigButton.center = center

      view.addSubview(bigButton)   
     }

     func hideButton() {
       bigButton.hidden = true   
     }

     func showButton() {
       bigButton.hidden = false
       view.bringSubviewToFront(bigButton)  
     }
}

FirstViewController.swift

class FirstViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    (tabBarController as! CustomTabBarController).addCenterButton()
  }

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    (tabBarController as! CustomTabBarController).showButton()
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let viewController = segue.destinationViewController as! UIViewController

    (tabBarController as! CustomTabBarController).hideButton()
    viewController.hidesBottomBarWhenPushed = true
  }
}
like image 396
Lucas Avatar asked May 16 '15 16:05

Lucas


1 Answers

I was dealing with a similar situation and ended up subclassing UITabBarController. In the subclass' viewDidLayoutSubviews method, calling bringSubviewToFront(view: UIView) and passing in the view that was going behind the tab bar fixed the issue. Make sure you call this method on the UITabBarController's view.

like image 85
bryanmikaelian Avatar answered Oct 07 '22 05:10

bryanmikaelian