Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Bar Not Showing after Adding NavigationController

Tags:

ios

swift

I have an existing UITableViewController that I've embedded in a NavigationController. However, the Navigation Bar is not showing when I present the view.

Presenting the TableViewController (its Storyboard id is: SelectServicesController) :

if let selectServicesController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectServicesController") as? UITableViewController {
    self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil)
}

enter image description here

This is what it looks like when I build (nav bar does not show):

enter image description here

like image 279
Onichan Avatar asked Feb 28 '16 23:02

Onichan


3 Answers

So I just did this and at fist could not get it to show up at all. Then Figured it out, You just need to select the navigation controller and set it to be the ✅is initial View Controller

This is what your storyboard should look like NavigationController setup

Then to make everything show up I added this to my viewDidLoad of the view the Navigation controller is presenting. This step is more optional.

    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()]
    navigationController?.navigationBar.hidden = false

And this is what it looks like

NavContorllerInAction

mmmm Red on black 🤓 Hope that helps you.

like image 175
Dan Leonard Avatar answered Sep 21 '22 02:09

Dan Leonard


You're presenting a UITableViewController, which doesn't have a navigation controller as a parent (even though your Storyboard has it first, you're not actually using it).

You can fix this by doing something like this:

if let selectServicesController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectServicesController") as? UITableViewController {
    let navigationController = UINavigationController(rootViewController: selectServicesController)
    self.navigationController?.presentViewController(navigationController, animated: true, completion: nil)
}

Or by setting the navigation controller as the initial view controller of the storyboard and then calling it like this:

if let selectServicesController = self.storyboard?.instantiateInitialViewController() {
    self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil)
}
like image 28
fdiaz Avatar answered Sep 18 '22 02:09

fdiaz


I encountered the same problem. I solved it by Changing the segue to the navigation controller that embeds the View Controller I want to display. enter image description here

Hopefully it would work for you. Let me know if it is a bad practice.

like image 32
The Doctor Avatar answered Sep 19 '22 02:09

The Doctor