Replacing MKMapView annotation with a popover

When working recently with a MKMapView I wanted to replace the usual annotation view with a popover. First try at this resulted in the popover displaying correctly but together with the annotation view.

I then tried turning off the annotation view using

view.canShowCallout = false

inside the function

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!

Although at first this seemed to give the desired result, the pin could not be selected a second time. The solution was to first ensure that the annotation had been deselected before presenting the popover.

func mapView(mapView: MKMapView!,didSelectAnnotationView view: MKAnnotationView!){

// deselect the annotation first self.mapView.deselectAnnotation(view.annotation, animated:true)

var popOverContent = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("MapPopOverVC") as UIViewController popOverContent.modalPresentationStyle = UIModalPresentationStyle.Popover popOverContent.popoverPresentationController?.sourceView = view popOverContent.popoverPresentationController?.sourceRect = view.bounds var detailPopover: UIPopoverPresentationController = popOverContent.popoverPresentationController! detailPopover.permittedArrowDirections = UIPopoverArrowDirection.Any presentViewController(popOverContent, animated:true, completion:nil) }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s