Here is my solution to this question:
Swift 5
First, add MKMapViewDelegate
method
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
This gets called when annotation is selected and the callout bubble is shown. You can extract the callout bubble view like this, and add the tap gesture recognizer to it, like so:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// Set action to callout view
guard let calloutView = view.subviews.first else { return }
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userDidTapAnnotationCalloutView(_:)))
calloutView.addGestureRecognizer(tapGesture)
}
And handle tap action like this:
@objc
private func userDidTapAnnotationCalloutView(_ sender: UITapGestureRecognizer) {
// Handle tap on callout here
}