In iOS 17, I am having an issue where UIPopoverPresentationControllers are not retaining the preferred size of the UIViewController they contain.
The following method uses a UIPopoverPresentationController popoverVC
to display a UIViewController contentVC
from a UIView view
anchor location frame
.
+ (void) showDialogiPad:(UIViewController *)contentVC
fromPresentingVC:(UIViewController *)presentingVC
inView:(UIView *)view
atFrame:(CGRect)frame
withDirection:(UIPopoverArrowDirection)direction {
// Set content view controller size
contentVC.preferredContentSize = contentVC.view.frame.size; // size should be 320x480
// Choose the presentation style in which the content is
// displayed in a popover view
contentVC.modalPresentationStyle = UIModalPresentationPopover;
// Set the popover size and anchor location
contentVC.popoverPresentationController.sourceRect = frame;
contentVC.popoverPresentationController.sourceView = view;
// Present the popover presentation controller
[presentingVC presentViewController:contentVC animated:YES completion:nil];
}
The contentVC
's size gets set in it's viewDidLoad
method as follows:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the size of this view controller (which is used in a popup view controller)
self.view.frame = CGRectMake(0, 0, 320, 480);
// Set a border around the view
CALayer *layer = self.view.layer;
layer.borderColor = [[UIColor darkGrayColor] CGColor];
layer.borderWidth = 2;
layer.cornerRadius = 10;
}
Prior to iOS 17, the code displayed the popover as expected as shown here:
Now however, since the update to iOS 17, the popover is now squashed and displayed as:
A few observations:
preferredContentSize
, the popover opens at the max size every time - so it shows all the content but it is not set to the size I want it to be.preferredContentSize
as is but change modalPresentationStyle
to UIModalPresentationFormSheet
, the size of the popover is correct but the popover always presents in the middle of the screen.I find that in many cases, instead of externally setting a view controller's preferredContentSize
property to some value, it works better if the given view controller overrides the preferredContentSize
property. This also has the benefit of putting the knowledge of the preferred size in the view controller itself.
- (CGSize)preferredContentSize {
return CGSizeMake(width: 320, height: 480);
}