Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationCon...

在项目开发中使用 popVC?.modalPresentationStyle = UIModalPresentationStyle.popover 做弹窗的时候出现了崩溃,报错

Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController 
(<UIPopoverPresentationController: 0x1042a5cc0>) should have a non-nil sourceView or barButtonItem set before the 
presentation occurs.'

我的代码

        let  popVC = TestPopViewController()
        popVC.popoverPresentationController?.barButtonItem = UIBarButtonItem(customView: customView)
        popVC.popoverPresentationController?.backgroundColor = UIColor(hexNumber: 0x000000, alpha: 0.5)
        popVC?.modalPresentationStyle = UIModalPresentationStyle.popover
        popVC?.popoverPresentationController?.delegate = self
        present(popVC, animated: true, completion: nil)

最后发现在使用Controller 的属性modalPresentationStyle 设置其样式以及给Controller设置其他属性的时候一定要先设置好modalPresentationStyle,然后再设置其他属性
正确的使用方法

        let  popVC = TestPopViewController()
        popVC?.modalPresentationStyle = UIModalPresentationStyle.popover
        popVC.popoverPresentationController?.barButtonItem = UIBarButtonItem(customView: customView)
        popVC.popoverPresentationController?.backgroundColor = UIColor(hexNumber: 0x000000, alpha: 0.5)
        popVC?.popoverPresentationController?.delegate = self
        present(popVC, animated: true, completion: nil)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容