Keywords: iOS 8 | UIPopoverPresentationController | preferredContentSize
Abstract: This article provides a comprehensive exploration of how to correctly customize the size of UIPopoverPresentationController in iOS 8. By examining common error cases, it explains why directly setting the popoverContentSize property fails and emphasizes the correct approach using the UIViewController's preferredContentSize property. Additionally, advanced techniques such as Storyboard Segue integration and adaptive presentation styles are discussed to help developers master best practices for popover presentation in iOS 8.
Introduction
In iOS 8, Apple introduced UIPopoverPresentationController as a modern replacement for UIPopoverController, offering more flexible popover presentation mechanisms. However, many developers encounter challenges when customizing sizes during migration. This article analyzes a typical technical Q&A case to delve into the root causes and provide systematic solutions.
Problem Analysis
When using UIPopoverPresentationController, developers often attempt to directly set the popoverContentSize property but find that the popover size does not change as expected. For example, the following code snippet illustrates this common mistake:
var popover = nav.popoverPresentationController as UIPopoverPresentationController
popover.popoverContentSize = CGSizeMake(1000, 300)Although the code logic seems reasonable, the popover size remains unadjusted at runtime, leading to abnormal interface presentation. The core issue lies in iOS 8's design shift: popover size should be determined by the content view controller, not by directly manipulating UIPopoverPresentationController.
Core Solution
The correct approach is to define the popover size by setting the preferredContentSize property of the content view controller. Here is the corrected code example:
func addCategory() {
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(500,600)
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}In this implementation, the preferredContentSize property is applied directly to the popoverContent view controller, and UIPopoverPresentationController automatically reads this value to adjust the popover size accordingly. This method not only resolves size customization issues but also aligns with iOS 8's MVC (Model-View-Controller) architecture principles, separating content from presentation logic.
Advanced Techniques and Supplements
Beyond direct programming, developers can leverage Storyboard Segues to simplify popover configuration. Create a Segue from the trigger control (e.g., UIBarButtonItem) to the target view controller in Storyboard, and implement the prepareForSegue method in the source view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentifierForPopOver" {
if let controller = segue.destinationViewController as? UIViewController {
controller.popoverPresentationController!.delegate = self
controller.preferredContentSize = CGSize(width: 320, height: 186)
}
}
}Additionally, for scenarios requiring iPhone adaptation, it is essential to implement the adaptivePresentationStyle method of UIPopoverPresentationControllerDelegate, returning UIModalPresentationStyle.none to ensure proper popover display on iPhones:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}Conclusion
In iOS 8, the key to correctly customizing UIPopoverPresentationController size lies in understanding its design philosophy: popover size should be controlled by the content view controller via the preferredContentSize property. This article elaborates on the correct usage by analyzing common error cases and provides advanced techniques for Storyboard integration and cross-device adaptation. Mastering these core concepts enables developers to implement flexible, platform-compliant popover interfaces more efficiently.