Search code examples
iosobjective-cstoryboarduistoryboardsegueios-universal-app

How to implement a UINavigationController for modal presentation, not for popover, with an adaptive segue


While using a universal storyboard and adaptive segues, how can one implement a Present As Popover segue that will have a navigation bar (with a title and close button) only on iPhone when presented modally, and will not have a navigation controller on iPad when presented as a popover?

I believe the proper setup is to not include a nav controller in the storyboard, control-drag to the new view controller and select a Present As Popover segue. Then in prepareForSegue one will need to create the navigation controller and embed the destination controller in it, then add the title and buttons, but only if it will be presented modally. If that approach is correct, how can one do that in code?


Solution

  • Rdelmar is correct, you can't do this in prepareForSegue as the destination view controller is already set.

    In iOS 7 and earlier you'll have to add that navigation controller to the storyboard and you could then have separate segues to the navigation controller and its root view. Then trigger the right segue on depending on whether you want the navigation controller (iPhone) or not (iPad).

    In iOS 8 you can use the new UIAdaptivePresentationControllerDelegate protocol and then create a navigation controller on the fly where you need it:

    func presentationController(controller: UIPresentationController!, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController! 
    {
      let presented = controller.presentedViewController
      return UINavigationController(rootViewController: presented)
    }