Swift - Present another view controller with its navigation bar

22,147

Solution 1

I fixed the problem using the following code:

let editorViewController = IMGLYMainEditorViewController()
let navEditorViewController: UINavigationController = UINavigationController(rootViewController: editorViewController)
self.presentViewController(navEditorViewController, animated: true, completion: nil)

I just added the navEditorViewController as it made my navigation bar with its items to appear.

Solution 2

Try self.navigationController!.pushViewController(...)

Solution 3

Swift 5+

let destinationNavigationController = self.storyboard!.instantiateViewController(withIdentifier: "nav") as! UINavigationController
destinationNavigationController.modalPresentationStyle = .fullScreen        
self.present(destinationNavigationController, animated: true, completion: nil)

Here your navigation bar replaces with new navigation bar.

Solution 4

So for everyone still curious about this problem, given that we already have existing UINavigationController other than the current one:

Swift 3

First, we need to find the UIViewController that we want to present:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController

Next, we're doing the same thing for UINavigationController:

let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "DestinationNavigationController") as! UINavigationController

Then, we want to bring the DestinationViewController to the top of our destination UINavigationController stack:

destinationNavigationController.pushViewController(destinationViewController, animated: true)

Finally, just present destination UINavigationController:

self.present(destinationNavigationController, animated: true, completion: nil)
Share:
22,147
Vasil Nunev
Author by

Vasil Nunev

Software Engineering student. Passionate programmer. Mostly self taught programmer. Now learning to develop iOS applications with swift 2.0. Doing personal projects to improve my skills. Looking for small projects.

Updated on May 02, 2020

Comments

  • Vasil Nunev
    Vasil Nunev almost 4 years

    I have two ViewControllers -- one with storyboard and one without. Both of those view controllers have their own Navigation Bar at the top. Now when I use self.presentViewController(editorViewController, animated: true, completion: nil) my editorViewController comes up but without its Navigation bar.

    Any ideas how to fix this?

  • David P
    David P about 8 years
    Probably because you haven't used a Navigation Controller. Can you show me your storyboard?
  • Vasil Nunev
    Vasil Nunev about 8 years
    Here is my storyboard postimg.org/image/5x0bszdlf when i press Edit the image editor editorViewController should pop up
  • David P
    David P about 8 years
    Sorry if I insist.. So your storyboard only has that view controller??
  • David P
    David P about 8 years
    I think you should use a NavigationController. Look here: postimg.org/image/byfh6263r
  • zulkarnain shah
    zulkarnain shah almost 7 years
    But the presented UIViewController doesn't have a Back button. How to handle that ?
  • Wimukthi Rajapaksha
    Wimukthi Rajapaksha almost 4 years
    @zulkarnainshah when you present a new navigation controller, your previous navigation stack get removed. if you need to show back button, then you have to present view controller, not a navigation controller.