Reputation: 5741
I'm trying to display a view controller as pop over, it works fine on the iPhone when it's displayed full screen, but crashes on the 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
}
The error message says:
Could not cast value of type 'UIViewController' (0x1b1e33f60) to 'Overpower.ItemSelectionVC' (0x10005ad40).
Upvotes: 2
Views: 1739
Reputation: 5741
Following @TonyMkenu's answer, I added these 2 lines of code to selectOpponentItems
, and it worked like magic:
VC.popoverPresentationController?.sourceView = sender
VC.popoverPresentationController?.sourceRect = sender.bounds
Upvotes: 7