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

在设置断点时,调试器一直运行到最后一行。之后,它直接进入AppDelegate班级第一行。

我已经正确设置了异常断点。我可能在哪里犯错?它与sourceViewfor相关popoverPresentationController吗?我不知道。

我想做的是设置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)

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

在此处输入图像描述

4

3 回答 3

2

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

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)

谢谢大家的努力。

于 2017-02-21T10:53:20.443 回答
1
Try this:
    let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
    obj.delegate = self
    obj.modalPresentationStyle = .overCurrentContext
    self.navigationController?.present(obj, animated: false, completion: {
                            })
于 2017-02-21T07:02:55.160 回答
1

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

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)

它会让你的弹出窗口出现在中心,没有箭头/锚点。

于 2017-02-21T10:48:59.090 回答