iOS中当适配iPad的时候使用UIAlertController注意的点
全栈老韩
全栈工程师,擅长iOS App开发、前端(vue、react、nuxt、小程序&Taro)开发、Flutter、React Native、后端(midwayjs、golang、express、koa)开发、docker容器、seo优化等。
使用UIAlertController时,要设置其popoverPresentationController.sourceView和popoverPresentationController.sourceRec这2个属性t,否则会导致适配iOS和iPad的app发生crash。
例如:
let alertController = UIAlertController(title: "My first app", message: "Hello World", preferredStyle: .ActionSheet)
//ipad使用,不加ipad上会崩溃
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
sourceView可以是UIButton、self.view等,也可以是barButtonItem;
例如:
if let popoverController = alertController.popoverPresentationController {
popoverController.barButtonItem = sender
}
self.presentViewController(alertController, animated: true, completion: nil)
发布于2024-02-02 11:04:31
浏览量45·
暂无评论,快来发表第一条评论吧