[Error]适配iPad时调用UIAlertController和UIActivityViewController软件崩溃问题
创始人
2024-03-17 00:35:02
0

问题:

适配iPad时,调用UIAlertController和UIActivityViewController软件崩溃。iPhone设备上软件运行正常。

错误打印如下:

Thread 1: "UIPopoverPresentationController () should have a non-nil sourceView or barButtonItem set before the presentation occurs."

Thread 1: "Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet from purecolor.RootTabBar (). 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和UIActivityViewController这类气泡类视图,在iPad上需要为其指定sourceView和sourceRect,以便让气泡的箭头指向sourceView。

解决:

UIAlertController在类型为actionSheet时会闪退,类型为alert时没影响。

@objc private func longPressViewAction(_ sender: Any) {guard let topVC = GAScreenUIManager.topViewController() as? LibraryViewController, topVC.className != "UIAlertController" else {return}let alert = UIAlertController(title: nil, message: tempMsg, preferredStyle: .actionSheet)if UIDevice.current.userInterfaceIdiom == .pad {var vcPoint = topVC.view.convert(self.center, to: topVC.view)vcPoint.y = vcPoint.y + UIDevice.navigationBarHeight() - topVC.collectionView.contentOffset.yalert.popoverPresentationController?.sourceView = topVC.viewalert.popoverPresentationController?.sourceRect = CGRect(x: vcPoint.x, y: vcPoint.y, width: 1, height: 1)alert.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.any}let anyAction = UIAlertAction(title: "复制名称和颜色", style: .default) { action in}alert.addAction(anyAction)let cancelAction = UIAlertAction(title: "取消", style: .cancel) { action in}alert.addAction(cancelAction)topVC.present(alert, animated: true, completion: nil)
}

UIActivityViewController

private func enterActivity(_ img: UIImage) {let activity = UIActivityViewController(activityItems: [img], applicationActivities: nil)if UIDevice.current.userInterfaceIdiom == .pad {activity.popoverPresentationController?.sourceView = self.viewactivity.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.size.width - 88, y:0 , width: 88, height: 88)activity.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.any}self.present(activity, animated: true)
}

示意图:

 

 

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...