使用UIAlertControllerStyleActionSheet崩溃

报错如下:

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.'

如图

写因:

这方面的崩溃信息较少,所以我要警醒世人勿入此坑。

入此坑的必备条件:

iPad开发时,使用UIAlertController的UIAlertControllerStyleActionSheep这个显示方式。

入坑后的分析结果:

运行设备为iPhone时,正常。
运行设备为iPad时,崩溃。


iPhone上显示

iPad上显示

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

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

代码如下:
// 解决iPad使用时的崩溃

UIPopoverPresentationController *popover = alertController.popoverPresentationController;

if (popover) {

    popover.sourceView = self.testBtn;
    popover.sourceRect = self.testBtn.bounds;
    popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
代码截图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容