iOS UIAlertController在iPhone与iPad上的区别

很简单的一段代码:

// 首先声明一个UIAlertController对象
private var alertController: UIAlertController!

// 初始化UIAlertController对象,中间省略弹出项
alertController = UIAlertController(title: self.selectDeviceModel?.name, message: nil, preferredStyle: .actionSheet)

// 弹出alertController
self.present(alertController, animated: true, completion: nil)

上面实现在屏幕底部弹出操作列表,在iPhone上面效果如下:

正常显示,所有的列表项及操作都没有问题。

可是就是这么一个简单的功能,在iPad上测试时,直接程序崩溃,输出了以下信息:

2018-08-07 09:25:41.064039+0800 InledcoLightSwift[2555:1299749] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x146819600>) 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.'

*** First throw call stack:

(0x185902d8c 0x184abc5ec 0x190105dd4 0x18f95cd8c 0x18f95af60 0x18f8ba8b8 0x18f8b098c 0x18f795550 0x1858aa910 0x1858a8238 0x1858a8884 0x1857c8da8 0x1877ab020 0x18f7a978c 0x10278f27c 0x185259fc0)

libc++abi.dylib: terminating with uncaught exception of type NSException

 

崩溃信息原因是你的程序弹出了一个actionSheet类型的alterController.后面说是一个modal类型的,你就必须提供弹出的位置信息,可以是一个View或者一个barButtonItem,也就是说这个弹出框的来源,要从哪里弹出来。上面说到的UIModalPresentationPopover还不知道什么样,只能试一试了。

那我们就按照提示信息设置一下看看都什么效果。

  1. 设置sourceView
alertController.popoverPresentationController?.sourceView = self.view;

效果如下

可以看到在屏幕左上角弹出了一个列表。

2.同时设置sourceRect

        alertController.popoverPresentationController?.sourceView = self.view;
        alertController.popoverPresentationController?.sourceRect = CGRect(x: 10.0, y: 10.0, width: 100.0, height: 100.0);

效果如图,在100, 100处弹出列表框

3.错误信息中还有一个barButtonItem,比如我设置self.navigationItem.rightBarButtonItem效果如下

综上所述,在iPad上是没有和iPhone上那种从底部弹出的效果的,我们只能设置sourceView或者barButtonItem来指定弹出的的位置。在iPad UIAlertController的类型为UIModalPresentationPopover,只能实现以上效果。

 

 

转载于:https://www.cnblogs.com/huangzhengguo/p/9436606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值