Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't presentationController:viewControllerForAdaptivePresentationStyle: being called?

Tags:

ios

iphone

I'm trying to programmatically present a view via an adaptive popover (e.g. in a popover on an iPad, full screen on an iPhone). In order to be able to dismiss the presented view controller on the iPhone, I've tried wrapping it in a navigation controller as in https://stackoverflow.com/a/29956631/5061277 or the nice example here: https://github.com/shinobicontrols/iOS8-day-by-day/tree/master/21-alerts-and-popovers/AppAlert, which looks like:

import UIKit

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

  @IBAction func handlePopoverPressed(sender: UIView) {
    let popoverVC = storyboard?.instantiateViewControllerWithIdentifier("codePopover") as! UIViewController
    popoverVC.modalPresentationStyle = .Popover
    // Present it before configuring it
    presentViewController(popoverVC, animated: true, completion: nil)
    // Now the popoverPresentationController has been created
    if let popoverController = popoverVC.popoverPresentationController {
      popoverController.sourceView = sender
      popoverController.sourceRect = sender.bounds
      popoverController.permittedArrowDirections = .Any
      popoverController.delegate = self
    }
  }

  func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    // This line _IS_ reached in the debugger
    NSLog("Delagate asked for presentation style");
    return .FullScreen
  }

  func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {

    // This line _IS_NOT_ reached in the debugger
    NSLog("Delegate asked for view controller");
    return UINavigationController(rootViewController: controller.presentedViewController)


  }
}

While the adaptivePresentationStyleForPresentationController delegate method is called, the presentationController viewControllerForAdaptivePresentationStyle delegate method is not called. As a result there is no way to dismiss the presented controller on an iPhone.

I've tried XCode 6.4 and 7.0b2 on iOS 8.1 through 8.4, both in the simulator and on a device, and in no case is my delegate asked for viewControllerForAdaptivePresentationStyle. Why? Is there a build setting I should be looking at, or could having XCode 7 installed be changing something? This exact code is presented as working in the links above.

like image 777
Chris Eveland Avatar asked Jul 04 '15 18:07

Chris Eveland


1 Answers

You need to present it after configuring it. Alternatively use a segue to make it much easier.

like image 104
malhal Avatar answered Oct 24 '22 02:10

malhal