Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force popover on iPhone with programmatically created UIPopoverPresentationController

Tags:

ios

swift

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.

like image 877
Shane Hsu Avatar asked Dec 29 '14 07:12

Shane Hsu


Video Answer


2 Answers

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

like image 159
gabbler Avatar answered Oct 02 '22 17:10

gabbler


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)
like image 35
Svitlana Avatar answered Oct 02 '22 17:10

Svitlana