我试图将视图控制器显示为弹出窗口,当iPhone全屏显示时,它可以在iPhone上正常工作,但在iPad上崩溃。
@IBAction func selectOpponentItems(_ sender: UIButton) {
let VC = storyboard?.instantiateViewController(withIdentifier: "ItemSelectionVC") as! ItemSelectionVC
// Error here
VC.delegate = self
VC.preferredContentSize = CGSize(width: UIScreen.main.bounds.width / 2, height: UIScreen.main.bounds.height / 2)
VC.modalPresentationStyle = UIModalPresentationStyle.popover
self.present(VC, animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
错误消息显示:
无法转换类型的值 '的UIViewController' (0x1b1e33f60)到' Overpower.ItemSelectionVC' (0x10005ad40)。
答案 0 :(得分:2)
按照@TonyMkenu的回答,我将这两行代码添加到selectOpponentItems
,它就像魔术一样:
VC.popoverPresentationController?.sourceView = sender
VC.popoverPresentationController?.sourceRect = sender.bounds