我正在尝试在图像上显示弹出框,但尝试将锚点指向我的图像边框(图像是信息图标(失败。这是我的代码:
按钮操作:
@IBAction func infoTapped(_ sender: AnyObject) {
self.performSegue(withIdentifier: "InfoPopOver", sender: nil)
}
和准备续集:
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)
}
}
这里有几个截图:主视图显示弹出框
我得到的最好的是手动设置坐标,但它并不适合每种屏幕类型......
我会把它改成这样:
vctr.popoverPresentationController?.sourceView = infoIcon.superview
vctr.popoverPresentationController?.sourceRect = infoIcon.frame
这样,您可以确定您使用的是 infoIcon 的超级视图,并且框架将处于正确的位置。