简体   繁体   中英

Popover Doesn't Work on iPhone

I implemented a Popover in my application and tested it. It will work fine on iPad but when I test it on iPhone, instead of showing Popover it will show the full storyboard. Please help me.

Here is my code:

@IBAction func MenuIsClick(sender: AnyObject) {
    var moveToNextVC:MenuViewController

    moveToNextVC = self.storyboard?.instantiateViewControllerWithIdentifier("MenuViewController") as! MenuViewController
    moveToNextVC.modalPresentationStyle = .Popover
    moveToNextVC.preferredContentSize = CGSizeMake(200, 200)

    if let popoverController = moveToNextVC.popoverPresentationController {
        popoverController.sourceView = sender as! UIView
        popoverController.sourceRect = CGRect(x: 0, y: 0, width: 85, height: 30)
        popoverController.permittedArrowDirections = .Any
        popoverController.delegate = self

    }
    presentViewController(moveToNextVC, animated: true, completion: nil)

}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!, traitCollection: UITraitCollection!) -> UIModalPresentationStyle {
    return .None
}

Edit: As Popover is not work on iPhone but when a refer this EthanStrider/iOS-Projects it will work fine. How?

UIPopoverController's are only available on iPad.

Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.

Though with iOS 8+, it no longer crashes, it will be executed as full screen modal presentation..

A UIPopoverController is meant for use only on iPad devices. You can refer to the Apple docs to verify.

If you really want a popover on iPhone, you can use a third-party control like WYPopoverController .

you can use this control else.

https://github.com/50pixels/FPPopover/

For me the problem was the .none parameter in this delegate method. When I added UIModalPresentationStyle in front of it - it started to work!

// Override the iPhone behavior that presents a popover as fullscreen
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

The reason was that I had another .none case defined in an enum in my project with another underlying Int value (which is of course bad practice, so please avoid that to begin with).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM