How to change the size of a popover
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.
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,
-
Add this to your view controller:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.preferredContentSize = self.view.systemLayoutSizeFitting( UIView.layoutFittingCompressedSize ) }
-
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".
icestorm0806
Updated on September 10, 2020Comments
-
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 almost 7 yearsWhilst 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 over 2 yearsOne small note from me: In my case the auto layout worked better when my
preferredContentSize
was calculated inviewDidLayoutSubviews
instead ofviewWillAppear
method.