简体   繁体   中英

Popover view controller crashes on iPad

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM