I\'m using Xcode 4.6.1 to code on Objective-C. I want to know how can I keep the navigation bar shown when I create a modal segue between 2 View controllers, because I\'m do
Here is my SHORTED version of "Volodymyr Nazarkevych" in Swift 4.2
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "segueIdentifier": // name of your segue
let navigation: UINavigationController = segue.destination as! UINavigationController
//here you create new name of your destination ViewController and assign his name to let constant above
guard let myNewViewController = navigation.viewControllers[0] as? DestinationViewController
//DestinationViewController is your 2-ViewController where you go to from first one.
else {
fatalError("Unexpected destination: \(segue.destination)")
}
// TO DO SOMETHING HERE
default:
fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
}
}
The code of "Volodymyr Nazarkevych" works perfectly, but only when your segue is from 1-ViewController to NavigationController of 2-ViewController, not directly to 2-ViewController. THANKS A LOT VOVA!.
Also in switch cases after destination block code you can do different stuff, for example to get some information or file from second ViewController.
In iOS 8 there is a better method. You can use adaptive presentation styles:
Objective-C:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationFullScreen;
}
- (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller.presentedViewController];
return navController;
}
Swift:
UIPopoverPresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.FullScreen
}
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
var navController = UINavigationController(rootViewController: controller.presentedViewController)
return navController
}
Swift 4:
extension MyViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.fullScreen
}
func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
return UINavigationController(rootViewController: controller.presentedViewController)
}
}
In prepare for segue method set the delegate:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let adpostVC = segue.destinationViewController as? XXXController {
let popPC = adpostVC.popoverPresentationController
popPC?.delegate = self
Modal segues take over the whole screen, so any navigation bars, tool bars, or tab bars that are in the presenting controller will be covered up. If you want a navigation bar on this modal controller, you'll need to add one specifically to it, and add any buttons you want to that new navigation bar (or tool bar). If you don't want to do this, then don't present it modally, do a push to it.
Just add another Navigation Controller
to your modal view controller. Follow the steps
Modal View Controller
Editor menu
Embed In
Navigation Controller
Run the application. Now it should work perfectly.
Hope this solves your problem.
swift version:
Follow steps:
Override prepareForSegue()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "goToYourController" {
let navigation: UINavigationController = segue.destinationViewController as! UINavigationController
var vc = YourViewController.init()
vc = navigation.viewControllers[0] as! YourViewController
//if you need send something to destnation View Controller
//vc.delegate = self
}
}