I have a view controller hierarchy and the top-most controller is displayed as a modal and would like to know how to display the navigation bar when using
\
try this
let transition: CATransition = CATransition()
let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.duration = 1
transition.timingFunction = timeFunc
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.view.window!.layer.addAnimation(transition, forKey: kCATransition)
self.presentViewController(vc, animated:true, completion:nil)
If you didn't set the modalPresentationStyle property (like to UIModalPresentationFormSheet), the navigation bar will be displayed always. To ensure, always do
[[self.navigationController topViewController] presentViewController:vieController
animated:YES
completion:nil];
This will show the navigation bar always.
Swift 3
let vc0 : ViewController1 = ViewController1()
let vc2: NavigationController1 = NavigationController1(rootViewController: vc0)
self.present(vc2, animated: true, completion: nil)
Swift version : This presents a ViewController which is embedded in a Navigation Controller.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// Identify the bundle by means of a class in that bundle.
let storyboard = UIStoryboard(name: "Storyboard", bundle: NSBundle(forClass: SettingsViewController.self))
// Instance of ViewController that is in the storyboard.
let settingViewController = storyboard.instantiateViewControllerWithIdentifier("SettingsVC")
let navController = UINavigationController(rootViewController: settingViewController)
presentViewController(navController, animated: true, completion: nil)
}
All a [self.navigationController pushViewController:controller animated:YES];
does is animate a transition, and add it to the navigation controller stack, and some other cool navigation bar animation stuffs. If you don't care about the bar animation, then this code should work. The bar does appear on the new controller, and you get an interactive pop gesture!
//Make Controller
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil
bundle:[NSBundle mainBundle]];
//Customize presentation
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.modalPresentationStyle = UIModalPresentationCurrentContext;
//Present controller
[self presentViewController:controller
animated:YES
completion:nil];
//Add to navigation Controller
[self navigationController].viewControllers = [[self navigationController].viewControllers arrayByAddingObject:controller];
//You can't just [[self navigationController].viewControllers addObject:controller] because viewControllers are for some reason not a mutable array.
Edit: Sorry, presentViewController will fill the full screen. You will need to make a custom transition, with CGAffineTransform.translation or something, animate the controller with the transition, then add it to the navigationController's viewControllers.
Swift 5.*
Navigation:
guard let myVC = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") else { return }
let navController = UINavigationController(rootViewController: myVC)
self.navigationController?.present(navController, animated: true, completion: nil)
Going Back:
self.dismiss(animated: true, completion: nil)
Swift 2.0
Navigation:
let myVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyViewController");
let navController = UINavigationController(rootViewController: myVC!)
self.navigationController?.presentViewController(navController, animated: true, completion: nil)
Going Back:
self.dismissViewControllerAnimated(true, completion: nil)