-1

I am trying to present a view controller modally and I want it to have a navigation bar.

I found a similar question here presenting ViewController with NavigationViewController swift

I do not want to change the RootViewController. I want either to add the navigationBar in the VC or to present modally already with one.

Thanks

Cublax
  • 1,232
  • 1
  • 11
  • 20

1 Answers1

3

You can always create a UINavigationController instance and add your view controller (the one you want to present modally) to it. Then, modally present the new UINavigationController instance instead of your view controller. This will show your view controller (because it is contained in the UINavigationController) with a navigation bar (because it is in a UINavigationController).

Here is an example:

let rootViewController = UITabBarController() // Lets assume this is your root view controller
let modalViewController = UIViewController() // Lets assume this is the view controller you want to present modally

// Here we wrap the modal controller in a navigation controller and then present it modally
let navigationController = UINavigationController(rootViewController: modalViewController)
rootViewController.present(navigationController, animated: true, completion: nil)
naglerrr
  • 2,809
  • 1
  • 12
  • 24