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

When using universal storyboards and adaptive segments, how can I implement the Present As Popover series, which will have a navigation bar (with a title and close button) only on the iPhone, if it is presented modally, and will not have a navigation controller on the iPad when presented in as a popover?

I believe that the correct setting is not to include the navigation controller in the storyboard, drag the control to the new view controller and select "Attend as Popover segue". Then, prepareForSegueyou will need to create a navigation controller and insert the destination controller into it, then add a title and buttons, but only if it is presented modally. If this approach is correct, how to do it in code?

+4
source share
2 answers

Rdelmar is right, you cannot do this in prepareForSeguebecause the destination view controller is already installed.

iOS 7 , . , (iPhone) (iPad).

iOS 8 UIAdaptivePresentationControllerDelegate, " " , :

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

, , - iPad, iPhone, .

Xcode 6.3 segue "Present as Popover"

, popover, popover:

popover:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "myPopoverSegueName") {
        let vc = segue.destinationViewController
        vc.popoverPresentationController?.delegate = self
        return
    }
}

/ :

extension myViewController: UIPopoverPresentationControllerDelegate {

    func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
        let btnDone = UIBarButtonItem(title: "Done", style: .Done, target: self, action: "dismiss")
        let nav = UINavigationController(rootViewController: controller.presentedViewController)
        nav.topViewController.navigationItem.leftBarButtonItem = btnDone
        return nav
    }

}

, :

func dismiss() {
    self.dismissViewControllerAnimated(true, completion: nil)
}
0

All Articles