很简单的一段代码:

// 首先声明一个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. Nodejs 使用Protobuf

    参考: Nodejs中使用Protobuf的.proto文件 为了在nodejs服务端使用proto 所以网上找了一些教程,这里实现下. 一 安装protobufjs 在服务端项目文件夹里shift+ ...

  2. C#串口小助手

    做技术的通病,什么都想学,什么都想亲手做一遍.不然总感觉心里不踏实. 考研期间,利用晚上一点时间,照葫芦画瓢,练习使用c#快速开发一个简单的串口小助手. 这种前后端分离的设计方法,大大提高了开发速度, ...

  3. Vue2.0 脚手架代码详解

    参考作者:https://www.jianshu.com/p/2b661d01eaf8 只是为了方便个人学习. 来看一下脚手架创建后的项目目录  说明:在*.vue文件,template标签里写htm ...

  4. filter滤镜效果(css3属性)

    <!DOCTYPE html> <html> <head> <style> img { width: 33%; height: auto; float: ...

  5. x86汇编

    ● 实模式下32bit处理器,与8086兼容,例如loop 指令结束标志,只判断cx,而不是ecx ● IO端口 实际上是一些寄存器,在IO接口当中.向外设读写数据,要通过读写IO端口来实现. 有些计 ...

  6. Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]

    题目链接:https://codeforces.com/contest/1089/problem/E Elma is learning chess figures. She learned that ...

  7. Codeforces 1043 - A/B/C/D/E/F - (Undone)

    链接:http://codeforces.com/contest/1043 A - Elections - [水水水水题] 题意: 我和另一个人竞争选举,共有 $n$ 个人投票,每个人手上有 $k$ ...

  8. python使用MySQLdb实现连接数据库Mysql

    python实现连接数据库mysql的步骤: 一.引入MySQLdb 二.获取与数据库的连接 三.执行SQL语句和存储过程 四.关闭数据库连接 1.什么是MySQLdb? MySQLdb是用于pyth ...

  9. 点击当前选项显示当前内容jquery

    <script language="javascript"> $(document).ready(function(){ $(".moren a") ...

  10. java之搭建webservice服务端

    1.新建工程. 2.配置项目名称和位置. 3.得到的项目结构如下: 4.配置tomcat服务器. (1) (2)新建一个tomcat服务. (3)配置tomcat参数. 5.配置WSDL文件. url ...