【Swift 5】使用UIAlertController弹出一个.actionSheet样式(UIAlertControllerStyleActionSheet)的弹出框,
运行设备为iPhone时,正常。
运行设备为iPad时,崩溃。
报错如下:
You must provide location information for this popover through the alert controller's popoverPresentationController

#完整报错信息
reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x124638680>) of style UIAlertControllerStyleActionSheet.
The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover.
You must provide location information for this popover through the alert controller's popoverPresentationController.
You must provide either a sourceView and sourceRect or a barButtonItem.
If this information is not known when you present the alert controller,
you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

解决:

iPhone和iPad使用UIAlertController展示ActionSheep时,展示是不一样的。

所以在iPhone上正常显示的AlertController,在iPad上显示要添加几行代码。

代码如下:

//解决ipad 弹出框崩溃问题 start
let popover:UIPopoverPresentationController? = alertController.popoverPresentationController;

if (popover != nil) {
popover!.sourceView = self.view;
popover!.sourceRect = self.view.bounds;
popover!.permittedArrowDirections = .any;
}
//解决ipad 弹出框崩溃问题 end