Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

presenting ViewController with NavigationViewController swift

I have system "NavigationViewController -> MyViewController", and I programmatically want to present MyViewController inside a third view controller. The problem is that I don't have navigation bar in MyViewController after presenting it. Can you help me?

var VC1 = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController") as ViewController self.presentViewController(VC1, animated:true, completion: nil) 
like image 749
Yury Alexandrov Avatar asked Aug 22 '14 09:08

Yury Alexandrov


People also ask

How do I present ViewController on navigation controller?

Set the modalPresentationStyle property of the new view controller to the desired presentation style. Set the modalTransitionStyle property of the view controller to the desired animation style. Call the presentViewController:animated:completion: method of the current view controller.


2 Answers

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.

like image 107
stefandouganhyde Avatar answered Sep 22 '22 05:09

stefandouganhyde


SWIFT 3

let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController let navController = UINavigationController(rootViewController: VC1) self.present(navController, animated:true, completion: nil) 
like image 37
Yaroslav Dukal Avatar answered Sep 20 '22 05:09

Yaroslav Dukal