Search code examples
swiftpopover

My popover extends all the whole way down to the bottom of the ViewController


I have spent a full day now building and re-building a simple popover view. But no matter what I do the popover view extends all the way down to the bottom of the ViewController the popover is displayed in. I'm a newbie but have googled this the whole day and can't find any solutions that works for me. I will appreciate any help! Find my code below:

ViewController:

class ViewController: UIViewController,
    UIPopoverPresentationControllerDelegate  {

...

@IBAction func studyButtonPressed(sender: AnyObject) {
    self.performSegueWithIdentifier("StudyPopover", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "StudyPopover" {
        let vc = segue.destinationViewController

        let controller = vc.popoverPresentationController

        if controller != nil {
            controller?.delegate = self
        }
    }
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
}

Here is a screenshot of the result. The popover is supposed to be 50 by 50, but it extends right down to the bottom of the ViewController.


Solution

  • So I have finally been able to find a solution for this issue. I had a breakthrough when I realised that the popover worked fine when on most device simulations but didn't work for iPhone 6 plus. Then I could narrow down my search for solutions and finally found that I had to add traitCollection: UITraitCollection to my adaptivePresentationStyleForPresentationController function.

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

    This makes the popover behave the same way either it's on a normal sized iPhone, iPhone 6 plus or iPad device.