UIAlertController在ipad上运行崩溃( UIAlertController (<UIAlertController: 0x1099a7800>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIMo)
当使用UIAlertController的
UIAlertControllerStyleActionSheet
时在ipad上运行会崩溃,报以下的错误:
reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x1099a7800>) 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.
意思就是没有设置UIAlertController这个弹出窗口的位置信息,可以通过下面的方式解决,
1 | alertSheetVc.popoverPresentationController.sourceView = self .bgScrollView; <br><br>alertSheetVc.popoverPresentationController.sourceRect = view.frame; |
或者是通过实现 UIPopoverPresentationControllerDelegate的prepareForPopoverPresentation方法 来设置UIAlertController在当前页面上的位置信息,设置后的显示效果与 iPhone是有区别的不是在屏幕的中间位置弹出而是在你所设置的位置弹出:如下图的界面效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | UIAlertController *alertSheetVc = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; alertSheetVc.popoverPresentationController.sourceView = self .bgScrollView; alertSheetVc.popoverPresentationController.sourceRect = view.frame; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@ "取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@ "去相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [ self pushTZImagePickerController]; }]; UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@ "拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [ self takePhoto]; }]; [alertSheetVc addAction:cameraAction]; [alertSheetVc addAction:albumAction]; [alertSheetVc addAction:cancelAction]; [ self presentViewController:alertSheetVc animated: YES completion: nil ]; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
2017-07-05 UIButton设置图片和标题上下垂直分布的总结