I'm trying to show a popover over an image and I'm failing trying to point the anchor point to my image border (the image is the info icon). Here is my code:
The button action:
@IBAction func infoTapped(_ sender: AnyObject) {
self.performSegue(withIdentifier: "InfoPopOver", sender: nil)
}
And the prepare for 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)
}
}
Here a couple of screenshots: Main View Popover shown
The best I got was setting the coordinates manually, but it doesn't fit neither every screen type...
I would change it to this:
vctr.popoverPresentationController?.sourceView = infoIcon.superview
vctr.popoverPresentationController?.sourceRect = infoIcon.frame
That way you can be sure that you are using the superview of the infoIcon and the frame will be in the correct position.