[Error]适配iPad时调用UIAlertController和UIActivityViewController软件崩溃问题
创始人
2024-03-17 01:21:24
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)
}

示意图:

 

 

相关内容

热门资讯

汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...
终于懂了手机麻将到底有没有挂... 您好:手机麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...