Presenting Viewcontroller with Navigationviewcontroller Swift

presenting ViewController with NavigationViewController swift

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.

Presenting ViewController of a Navigation Controller

Try this

@IBAction func save(_ sender: Any) {
//text fields cant be nil
guard let title = titleTextField.text, let summary = summarizeTextView.text else { return }

//save item
let item = NSEntityDescription.insertNewObject(forEntityName: "Book", into: managedObjectContext) as? Book
item?.title = title
item?.summary = summary
item?.date = NSDate()
print("Transaction successfully saved!")

//dismiss view
print("Parent: \(parent)")
print("Presenting: \(presentingViewController)")
print("Parents presenting: \(parent?.presentingViewController?.presentingViewController)")
print("parent of parent: \(parent?.parent)")
if let presentedBy = presentingViewController as? UINavigationController, let rootVC = presentedBy.viewControllers.first as? TableViewController {
rootVC.changeNavTitle()
dismiss(animated: true, completion: nil)
}
}

How to present view controller embedded in navigation controller modally?

Refer to this answer here, Option 2: Presenting ViewController with NavigationController

Simply, you need to embed a navigationController into vc3 and then present the navigation controller modally. You will need to create your own back button though.

How to present view controller modally while keeping navigation bar present. (For a settings view controller)

Create a navigation controller with root view controller as your view controller and then present navigation controller. Something like this.

let vc =  // your view controller 
let nav = UINavigationController(rootViewController: vc)
self.present(nav, animated: true, completion: nil)

Present ViewController modally with navigation Bar not changing the stackView

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)

presenting viewController with transition

You may go with Following,

 let sw  = revealViewController()
self.view.window?.rootViewController = sw
let viewControllerModelos = storyboard!.instantiateViewController(withIdentifier: "vc_catalogo_familias") as! VC_catalogo
let navigationController = UINavigationController(rootViewController: viewControllerModelos)
navigationController.navigationBar.isHidden=false
navigationController.setNavigationBarHidden(true, animated: false)
let trans = CATransition()
trans.duration = 0.5
trans.type = kCATransitionPush
trans.subtype = kCATransitionFromLeft
trans.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
sw.view.window!.layer.add(trans, forKey: kCATransition)
sw!.setFront(navigationController, animated: true)


Related Topics



Leave a reply



Submit