The error message is telling you that you need to give the alert controller’s popoverPresentationController
a location so that it can position itself properly. This is easy to do — just check to see if there’s a popover controller and add the sender as the source.
If your button is a UIBarButtonItem
:
if let popoverController = alertController.popoverPresentationController {
popoverController.barButtonItem = sender
}
self.presentViewController(alertController, animated: true, completion: nil)
Otherwise:
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = sender
popoverController.sourceRect = sender.bounds
}
self.presentViewController(alertController, animated: true, completion: nil)