Swift3:呈现弹出时崩溃

Swift3: Crash on presenting pop over

let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
obj.delegate = self

obj.modalPresentationStyle = .popover
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
self.present(obj, animated: true, completion: nil)

在设置断点时,调试器运行良好,直到最后一行。之后直接到AppDelegateclass第一行。

我已经正确设置了异常断点。我可能在哪里犯错? popoverPresentationControllersourceView有关系吗?我不确定。

我想做的是在中间设置 popoverPresentationController。有帮助吗?

编辑: 我将 sourceView 添加到代码中,如下所示,现在可以正常工作了:

obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)

但是,它不在屏幕中央。把屏幕截图供参考。我如何使其到达中心并移除方向箭头?

Try this:
    let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
    obj.delegate = self
    obj.modalPresentationStyle = .overCurrentContext
    self.navigationController?.present(obj, animated: false, completion: {
                            })

您必须结合使用 sourceViewsourceRect 来为弹出提供锚点,如下所示:

obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)

此外,如果您不希望锚点箭头在那里,请使用:

obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0)

它会让你的弹出窗口出现在中心,没有 arrow/anchor 点。

进行以下代码更改后,我能够使其正常工作。

let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
obj.delegate = self

obj.modalPresentationStyle = .popover
obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0)
obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)
self.present(obj, animated: true, completion: nil)

感谢大家的努力。