13

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?

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Vasil Nunev
  • 450
  • 1
  • 4
  • 16
  • Vasil, for your question you just deleted, try looking at Toucan: https://github.com/gavinbunney/Toucan – brimstone Mar 27 '16 at 03:07

4 Answers4

28

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.

Vasil Nunev
  • 450
  • 1
  • 4
  • 16
  • But the presented UIViewController doesn't have a Back button. How to handle that ? – zulkarnain shah May 09 '17 at 09:53
  • @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. – Wimukthi Rajapaksha May 01 '20 at 12:17
9

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

David P
  • 1,023
  • 10
  • 17
5

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.

Wimukthi Rajapaksha
  • 961
  • 1
  • 11
  • 23
3

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)
Chris Tsitsaris
  • 590
  • 10
  • 12