15
votes

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.

2

2 Answers

9
votes

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

1
votes
let contentView  = 
PLMainNavigationManager.sharedInstance.storyboard.instantiateViewControllerWithIdentifier("PLSearchVCID") as! PLSearchVC
        contentView.modalPresentationStyle = UIModalPresentationStyle.Popover
        contentView.preferredContentSize = CGSizeMake(400.0, 500.0)
        var _popoverPresentationController = contentView.popoverPresentationController!
        _popoverPresentationController.delegate = self
        _popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.Any
        _popoverPresentationController.sourceView = self.view
       _popoverPresentationController.sourceRect = CGRectMake(-30, -280, 320, 400)  
        PLMainNavigationManager.sharedInstance.navigationController?.presentViewController(contentView, animated: true, completion: nil)