iOS中当适配iPad的时候使用UIAlertController注意的点

原创文章
声明:作者声明此文章为原创,未经作者同意,请勿转载,若转载,务必注明本站出处,本平台保留追究侵权法律责任的权利。
全栈老韩
全栈工程师,擅长iOS App开发、前端(vue、react、nuxt、小程序&Taro)开发、Flutter、React Native、后端(midwayjs、golang、express、koa)开发、docker容器、seo优化等。

使用UIAlertController时,要设置其popoverPresentationController.sourceViewpopoverPresentationController.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)

暂无评论,快来发表第一条评论吧