Onichan
Onichan

Reputation: 4516

Navigation Bar Not Showing after Adding NavigationController

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

Upvotes: 6

Views: 15495

Answers (10)

Nexus242
Nexus242

Reputation: 619

Adding this works for me:

self.navigationController?.isNavigationBarHidden = false

Upvotes: 2

Mirza Q Ali
Mirza Q Ali

Reputation: 747

let storyboard = UIStoryboard(name: "Expense", bundle: Bundle(for: PTCAddExpenseViewController.self))


let controller =  storyboard.instantiateViewController(withIdentifier:"AddExpense") as! PTCAddExpenseViewController



let navigationController = UINavigationController(rootViewController: controller)

self.present(navigationController, animated: true, completion: nil)

Upvotes: 1

Steve W.
Steve W.

Reputation: 81

Or it might be this!!

I had the same problem: the navigation bar was showing on the root view in Storyboard, but when running the Simulator - there was no navigation bar at the top of the views. This solved it:

Navigation Controller > Navigation Bar > UNCHECK Translucent (it is checked by default). This did two things:

  1. My Navigation Bar shows on all subsequent views.
  2. The topmost subview is now at Y=0, and not Y=64.

enter image description here

Upvotes: 0

The Doctor
The Doctor

Reputation: 504

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.

Upvotes: 3

JYeh
JYeh

Reputation: 4333

I read in one of your comments you want to present the table view controller modally, with the navigation bar showing. We can do this using the Storyboard. From the view controller that should display this table view controller modally, Ctrl+Drag from the view controller to the Navigation Controller of the Table View Controller. Then, select Present Modally. Set an Identifier for the segue. Then, in your code for the view controller that is presenting the table view controller modally, call:

self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil)

Another way to do this without having to use any code is if the modal presentation is being triggered by something like a button. Then, you can Ctrl+Drag from that button to the Navigation Controller and select Present Modally.

Upvotes: 0

fdiaz
fdiaz

Reputation: 2600

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)
}

Upvotes: 5

Dan Leonard
Dan Leonard

Reputation: 3395

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.

Upvotes: 5

paulvs
paulvs

Reputation: 12053

In your code, change this line

self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil)

to this

self.presentViewController(selectServicesController, animated: true, completion: nil)

Upvotes: 0

cyril
cyril

Reputation: 2995

My guess is Xcode is ignoring the fact that your table view controller is embedded in navigation controller when presenting your table view controller with the following code:

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

Instead, I would suggest you modify the Top Bar setting under Simulated Metrics to suit your needs or instantiate your navigation controller instead (the latter is preferred and recommended)

Upvotes: 0

NRitH
NRitH

Reputation: 13893

You're presenting the table view controller directly, not its navigation controller. If you mark the nav controller as the initial view controller (tick the "Is initial view controller" box in the attributes inspector), then you can instantiate and show it by:

if let selectServicesNavController = self.storyboard?.instantiateInitialViewController() as? UINavigationController {
    // if you're pushing it onto an existing nav controller
    self.navigationController?.presentViewController(selectServicesNavController, animated: true, completion: nil)

    // if not (and this is probably the case), set the nav controller as your window's rootViewController
    UIApplication.sharedApplication().keyWindow.rootViewController = selectServicesNavController
}

Upvotes: 0

Related Questions