presenting ViewController with NavigationViewController swift
My navigation bar was not showing, so I have used the following method in Swift 2 iOS 9
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as! Dashboard
// Creating a navigation controller with viewController at the root of the navigation stack.
let navController = UINavigationController(rootViewController: viewController)
self.presentViewController(navController, animated:true, completion: nil)
SWIFT 3
let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
let navController = UINavigationController(rootViewController: VC1)
self.present(navController, animated:true, completion: nil)
Calling presentViewController
presents the view controller modally, outside the existing navigation stack; it is not contained by your UINavigationController or any other. If you want your new view controller to have a navigation bar, you have two main options:
Option 1. Push the new view controller onto your existing navigation stack, rather than presenting it modally:
let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
self.navigationController!.pushViewController(VC1, animated: true)
Option 2. Embed your new view controller into a new navigation controller and present the new navigation controller modally:
let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.present(navController, animated:true, completion: nil)
Bear in mind that this option won't automatically include a "back" button. You'll have to build in a close mechanism yourself.
Which one is best for you is a human interface design question, but it's normally clear what makes the most sense.