Swift 弹出框锚点

Swift Popover Anchor

我正在尝试在图像上显示弹出窗口,但未能尝试将锚点指向我的图像边框(图像是信息图标)。 这是我的代码:

按钮动作:

@IBAction func infoTapped(_ sender: AnyObject) {
        self.performSegue(withIdentifier: "InfoPopOver", sender: nil)
    }

并为 segue 做准备:

extension AddExpenseViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "InfoPopOver" {
            if let vctr = segue.destination as? MyPopOverViewController {
                vctr.modalPresentationStyle = .popover
                vctr.popoverPresentationController?.delegate = self
                vctr.popoverPresentationController?.sourceView = self.view
                vctr.popoverPresentationController?.sourceRect = infoIcon.frame

            }
        }
    }

}

class MyPopOverViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.preferredContentSize = CGSize(width: 185, height: 80)

    }
}

这里有几张截图: Main View Popover shown

我最好的办法是手动设置坐标,但它并不适合所有屏幕类型...

我会改成这样:

vctr.popoverPresentationController?.sourceView = infoIcon.superview
vctr.popoverPresentationController?.sourceRect = infoIcon.frame

这样您就可以确定您正在使用 infoIcon 的父视图并且框架将处于正确的位置。