Present ViewController in front of UITabBarController's tabBar and hide this tabBar

vanelizarov picture vanelizarov · Jul 8, 2015 · Viewed 17.7k times · Source

In my project I have a UITabBarController. And on one of it's ViewControllers I have button. When I click this button, a new ViewController is presenting modally.

The problem is, when the second VC is presenting, tabBarController's tabBar is still visible. When I try to hide it in first ViewController's action openFiltersList() with this method:

self.tabBarController?.tabBar.hidden = true

it hides, but when I'm trying to unhide it, when I dismiss second VC, setting this parameter to falsedoesn't work, tabBar stays hidden. Here's the code for first and second:

First (InvitesViewController, one of the tabBarController's View Controllers):

func openFiltersList() {

        var filtersView : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("filtersViewController") as! FiltersViewController
        filtersView.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext


        self.presentViewController(filtersView, animated: true) { () -> Void in

            UIView.animateWithDuration(0.3, animations: { () -> Void in

                filtersView.view.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.5)

            })

        }

        self.tabBarController?.tabBar.hidden = true

    }

Second (FiltersViewController, not embedded anywhere):

@IBAction func dismiss(sender: AnyObject) { // close button action

        self.dismissViewControllerAnimated(true, completion: nil)

        var destinationVC : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("invitesViewController") as! InvitesViewController
        destinationVC.tabBarController?.tabBar.hidden = false

    }

I'm using storyboard for interface.

Answer

DigitalBrain_DEV picture DigitalBrain_DEV · Jul 8, 2015

You should present the new viewController from tab bar controller:

 self.tabBarController?.presentViewController(filtersView, animated: true) { () -> Void in

        UIView.animateWithDuration(0.3, animations: { () -> Void in

            filtersView.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)

        })

    }