弹出视图控制器在 iPad 上崩溃

Bri*_*ure 2 ios swift

我正在尝试将视图控制器显示为弹出窗口,当它全屏显示时,它在 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
}
Run Code Online (Sandbox Code Playgroud)

错误消息显示:

无法将类型“UIViewController”(0x1b1e33f60)的值转换为“Overpower.ItemSelectionVC”(0x10005ad40)。

Bri*_*ure 7

按照 @TonyMkenu 的回答,我将这两行代码添加到selectOpponentItems,它就像魔术一样工作:

VC.popoverPresentationController?.sourceView = sender
VC.popoverPresentationController?.sourceRect = sender.bounds
Run Code Online (Sandbox Code Playgroud)