Ryan R
Ryan R

Reputation: 8461

Perform segue to another Navigation Controller without showing Tab Bar

I have a root Tab Host Controller with two Navigation Controller tab siblings: (1) Nearby Stops and (2) Saved Stops. Each of these has a View Controller respectively.

I would like to perform a segue from one of the sibling View Controllers to another Navigation Controller with Stop Schedule View Controller embedded in it, with the following requirements:

  1. The root Tab Bar should not show at the bottom of this View Controller
  2. I need to pass a Stop object to this View Controller before performing the segue

Storyboard: enter image description here

Currently, I am performing a segue this way, though the Tab Bar remains on the Stop Schedule View Controller when it shouldn't.

func showStopSchedule(stop: Stop) {
    let stopScheduleController = self.storyboard?.instantiateViewControllerWithIdentifier("StopScheduleViewController") as! StopScheduleViewController

    stopScheduleController.stop = stop    // pass data object

    self.navigationController?.pushViewController(stopScheduleController, animated: true)
}

Upvotes: 1

Views: 3004

Answers (3)

Mahdi Pedram
Mahdi Pedram

Reputation: 749

If you want to only hide the navigationController the below code works.

  self.navigationController?.navigationBar.hidden = true

Upvotes: 1

Paulw11
Paulw11

Reputation: 115076

You can simply set the hidden property of your tab bar when the stop schedule view controller is displayed and unhide the tab bar before that view controller disappears

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.tabBar.hidden=true
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.tabBarController?.tabBar.hidden=false
}

Update: To animate the transition you can use this:

class StopViewController: UIViewController {

    var barFrame:CGRect?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)
        // self.tabBarController?.tabBar.hidden=true
        if  let tabBar=self.tabBarController?.tabBar {
           self.barFrame=tabBar.frame

           UIView.animateWithDuration(0.3, animations: { () -> Void in
               let newBarFrame=CGRectMake(self.barFrame!.origin.x, self.view.frame.size.height, self.barFrame!.size.width, self.barFrame!.size.height)
               tabBar.frame=newBarFrame
            }, completion: { (Bool) -> Void in
                tabBar.hidden=true
            })

        }
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        self.tabBarController?.tabBar.hidden=false;
        if self.barFrame != nil {
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                let newBarFrame=CGRectMake(self.barFrame!.origin.x, self.view.frame.size.height-self.barFrame!.size.height, self.view.frame.size.width, self.barFrame!.size.height)
                self.tabBarController?.tabBar.frame=newBarFrame
            })

        }
    }
}

enter image description here

Upvotes: 4

SwiftArchitect
SwiftArchitect

Reputation: 48552

You are not using the segue you just defined in your Storyboard. Instead, you are currently reloading your StopScheduleViewController manually, whereas you should only perform the segue you already have defined.

Add an Identifier to each of the Storyboard Segue you want to invoke programmatically,

segue identifier

then load them in this manner:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("showStopSchedule", sender: self)
}

Use Storyboard segue animation

Upvotes: 1

Related Questions