在項目開發(fā)中使用 popVC?.modalPresentationStyle = UIModalPresentationStyle.popover 做彈窗的時候出現(xiàn)了崩潰,報錯
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)
最后發(fā)現(xiàn)在使用Controller 的屬性modalPresentationStyle 設(shè)置其樣式以及給Controller設(shè)置其他屬性的時候一定要先設(shè)置好modalPresentationStyle,然后再設(shè)置其他屬性
正確的使用方法
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)