很简单的一段代码:

// 首先声明一个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,只能实现以上效果。

iOS UIAlertController在iPhone与iPad上的区别的更多相关文章

  1. ios 新建app iphone 、 ipad or universal ?

    很久没有关注这个新建app的  时候 选什么的问题了, 因为我们一般在公司 都是 已经建立好的app 直接 在那上面开发. 所以很久不建立新app 遇到新的app需要你自己去创建的时候 可能就会 有突 ...

  2. IOS开发之Iphone和Ipad应用程序图标和启动动画

    本文转载至 http://blog.csdn.net/yesjava/article/details/8782060 当我们用xcode开发iphone和ipad应用程序的时候,我们可以用一下表中所显 ...

  3. 如何在iPhone与iPad上开启firebug

    原文: MARTIN KOOL games - web - dad - sarien.net - q42 - livejs - handcraft How to use Firebug on your ...

  4. IOS - IPhone或IPAD,如何恢复出厂操作系统?

    IPhone或IPAD的操作系统都是IOS,如果IPhone或IPAD越狱,或其它原因导致不能正常使用了,恢复出厂设置能够得到一个可以正常工作的设备.恢复的方法也比较简单,就是用iTunes,一般情况 ...

  5. 【转】越狱的 iPhone、iPad 通过网站实现一键安装 ipa 格式的 APP 应用

    1.已经越狱的 iPhone.iPad 设备,当通过其自带的 safari 浏览器访问 ipa 应用下载网站时,利用 itms-services 协议,可以一键安装 ipa 文件的 iOS 应用,例如 ...

  6. iPhone与iPad开发实战读书笔记

    iPhone开发一些读书笔记 手机应用分类1.教育工具2.生活工具3.社交应用4.定位工具5.游戏6.报纸和杂志的阅读器7.移动办公应用8.财经工具9.手机购物应用10.风景区相关应用11.旅游相关的 ...

  7. 在Mac电脑上为iPhone或iPad录屏的方法

    在以前的Mac和iOS版本下,录制iPhone或者iPad屏幕操作是一件稍微复杂的事情.但是随着Yosemite的出现,在Mac电脑上为iPhone或iPad录屏的方法就变得简单了.下面就介绍一下具体 ...

  8. iPhone与iPad在开发上的区别

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. 通过Mac远程调试iPhone/iPad上的网页(转)

    我们知道在 Mac/PC 上的浏览器都有 Web 检查器这类的工具(如最著名的 Firebug)对前端开发进行调试,而在 iPhone/iPad 由于限于屏幕的大小和触摸屏的使用习惯,直接对网页调试非 ...

随机推荐

  1. Python——rrdtool模块的安装

    一.Centos环境yum命令安装 yum install rrdtool rrdtool-devel 二.源码安装 wget https://pypi.python.org/packages/99/ ...

  2. Centos6.8 安装dlib库时出错【升级gcc 到4.9.0以上】

    在centos6.8上安装dlib库时出现错误: 1.CMake must be installed to build the following extensions: dlib 没有安装CMake ...

  3. GBDT 详解分析 转+整理

    GBDT DT 回归树 Regression Decision Tree 梯度迭代 GBDT工作过程实例 需要解释的三个问题 - 既然图1和图2 最终效果相同,为何还需要GBDT呢? - Gradie ...

  4. 11.15luffycity(7)

    2018-11-15 17:43:50 还有一点路飞项目就结束啦! 周日打算回去! 双十一的耳机到啦,音质确实不错!2333! 等着项目做完,完整总结一下! 越努力,越幸运!永远不要高估自己!!!! ...

  5. QT上位机

    程序是用QT写的,通过COM口进行数据的读取. 源码地址:https://github.com/kunkunlin/QT-SWJ

  6. 固定浮动侧边栏(SmartFloat)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 理解linux 密码存储

    1. 传统上,linux把加密(哈希)的密码保存在/etc/passwd文件中,passwd文件的格式如下: smithj:x:561:561:Joe Smith:/home/smithj:/bin/ ...

  8. ubuntu 16.04 安装和配置vncserver

    https://www.linode.com/docs/applications/remote-desktop/install-vnc-on-ubuntu-16-04/#connect-to-vnc- ...

  9. 大数据和Hadoop时代的维度建模和Kimball数据集市

    小结: 1. Hadoop 文件系统中的存储是不可变的,换句话说,只能插入和追加记录,不能修改数据.如果你熟悉的是关系型数据仓库,这看起来可能有点奇怪.但是从内部机制看,数据库是以类似的机制工作,在一 ...

  10. html form禁止表单回车自动提交(通常原因是为在ajax提交前、后进行js判断控制)

    @*onkeydown事件用于禁止回车自动提交form,这样就不经过js控制跳转*@ <form action="/Account/CheckPsw" method=&quo ...