Modal segue, navigation bar disappears

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 doing the segue in the storyboard and when I run the application the navigation bar of the second view controller disappears, and I have a done button on that bar but I can't see it.


Solution 1:

Just add another Navigation Controller to your modal view controller. Follow the steps

  1. Select the Modal View Controller
  2. Go to Editor menu
  3. Select Embed In
  4. Select Navigation Controller

Run the application. Now it should work perfectly.

Hope this solves your problem.

Solution 2:

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.

Solution 3:

You could simply do the following to show the navigation bar:

Objective-C

UINavigationController alloc]initWithRootViewController:modalVC];

SWIFT 3

let modelVC = self.storyboard?.instantiateViewController(withIdentifier: "modalVC") as! ModalVC
let navBarOnModal: UINavigationController = UINavigationController(rootViewController: modalVC)
self.present(navBarOnModal, animated: true, completion: nil)

This will show the modal view controller with the navigation bar. Knowledge on Objective-C is limited, so I did not write the part of presenting. You should be able to figure that one out. ;)

Hope this helps!

Solution 4:

In iOS 8 there is a better method. You can use adaptive presentation styles:

  1. Use a "Present as popover" segue
  2. Set your controller to adopt the UIPopoverPresentationControllerDelegate protocol
  3. Implement 2 methods

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