Shane Hsu Shane Hsu - 1 year ago 135
Swift Question

Force popover on iPhone with programmatically created UIPopoverPresentationController

I need to create a new popover view as the anchor is not visible in Interface Builder during compile time.

According to this post, implementing the delegate method will force Popover on iPhone. (for reasons I don't understand)

It works great when on a segue as shown in the post. But I cannot get this to work in a non-segue way. (Popover does show up on iPad)

Please help!

The code is listed below:

func showOptions() {
let contentView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PasteOption") as NewPasteOptionViewController
contentView.modalPresentationStyle = UIModalPresentationStyle.Popover
contentView.preferredContentSize = CGSizeMake(200.0, 200.0)
presentViewController(contentView, animated: true, completion: nil)
var _popoverPresentationController = contentView.popoverPresentationController!
_popoverPresentationController.delegate = self
_popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Any
_popoverPresentationController.sourceView = view
_popoverPresentationController.sourceRect = self.navigationItem.titleView!.frame
// this function below is never called.
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None

Additional Notes:

The presenting view controller is presented as a modal form sheet in another view, and encapsulated in a navigation controller.
The presented view controller is a custom table view controller.

Answer Source

It seems moving the line presentViewController(contentView, animated: true, completion: nil) to the end of the function would fix the issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download