Swift - Present another view controller with its navigation bar
Asked Answered
D

4

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?

Digenesis answered 22/3, 2016 at 13:49 Comment(1)
Vasil, for your question you just deleted, try looking at Toucan: github.com/gavinbunney/ToucanRecognizance
D
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.

Digenesis answered 22/3, 2016 at 14:38 Comment(2)
But the presented UIViewController doesn't have a Back button. How to handle that ?Masturbate
@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.Xeniaxeno
S
9

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

Strengthen answered 22/3, 2016 at 13:58 Comment(4)
Probably because you haven't used a Navigation Controller. Can you show me your storyboard?Strengthen
Here is my storyboard postimg.org/image/5x0bszdlf when i press Edit the image editor editorViewController should pop upDigenesis
Sorry if I insist.. So your storyboard only has that view controller??Strengthen
I think you should use a NavigationController. Look here: postimg.org/image/byfh6263rStrengthen
X
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.

Xeniaxeno answered 1/5, 2020 at 12:26 Comment(0)
G
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)
Glyptography answered 15/3, 2018 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.