How to change the size of a popover

44,363

Solution 1

Set the preferred content size on the view controller being presented not the popoverPresentationController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) // func for popover
    {
        if segue.identifier == "popoverView"
        {
            let vc = segue.destinationViewController

            vc.preferredContentSize = CGSize(width: 200, height: 300)

            let controller = vc.popoverPresentationController

            controller?.delegate = self
            //you could set the following in your storyboard
            controller?.sourceView = self.view
            controller?.sourceRect = CGRect(x:CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds),width: 315,height: 230)
            controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)

        }
    }

Solution 2

I fixed it via storyboard : Click on your controller Click on Attribute inspector ViewController> Check Use Preferred Explicit size and input values. enter image description here

Solution 3

Using Auto Layout

It may be worth mentioning that you can use layout constraints instead of setting preferredContentSize to specific values. To do so,

  1. Add this to your view controller:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.preferredContentSize = self.view.systemLayoutSizeFitting(
            UIView.layoutFittingCompressedSize
        )
    }
    
  2. Ensure that you have constraints from your popover view to the controller's root view. These can be low priority, space >= 0 constraints.

Solution 4

Above answers are correct which states about using the preferredContentSize, but the most important thing is to implement the protocol UIPopoverPresentationControllerDelegate and implement the below method otherwise it will not change the content size as expected.

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

Solution 5

Similar to Xeieshan's answer above, I set this via the storyboard.
Except that "Presentation" also needed to be to "Form Sheet".

enter image description here

Share:
44,363
icestorm0806
Author by

icestorm0806

Updated on September 10, 2020

Comments

  • icestorm0806
    icestorm0806 almost 4 years

    I'm having trouble changing the size of my popover presentation. Here is what I have so far

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) // func for popover
    {
        if segue.identifier == "popoverView"
        {
            let vc = segue.destinationViewController
    
            let controller = vc.popoverPresentationController
    
            if controller != nil
            {
                controller?.delegate = self
                controller?.sourceView = self.view
                controller?.sourceRect = CGRect(x:CGRectGetMidX(self.view.bounds), y: CGRectGetMidY(self.view.bounds),width: 315,height: 230)
                controller?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
            }
        }
    }
    

    So far all this does is center the popover and remove the arrow, which is good. but it doesn't resize the container. any help would be greatly appreciated. thank you.

    when I use preferredContentSize I get the error "Cannot assign to property: 'preferredContentSize' is immutable"

  • GhostCat
    GhostCat almost 7 years
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • dvp.petrov
    dvp.petrov over 2 years
    One small note from me: In my case the auto layout worked better when my preferredContentSize was calculated in viewDidLayoutSubviews instead of viewWillAppear method.