Swift Popover Anchor

时间:2017-06-12 06:20:13

标签: ios swift uikit popover anchorpoint

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

按钮操作:

@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

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

1 个答案:

答案 0 :(得分:0)

我会改为:

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

通过这种方式,您可以确定使用的是infoIcon的超级视图,并且框架将处于正确的位置。