Thursday, February 9, 2012

iOS 5.0 MKMapView (MapKit MapView) Edge Cases

The following are notes from my adventures implementing complex custom callouts in an MKMapView on iOS 5.0.

  • The selected MKAnnotationView is brought to the front of the group.
  • When the selected MKAnnotationView is deselected, other undefined annotations may appear in front.
  • Calling -[MKMapView selectAnnotation:animated:] on an annotation with a disabled MKAnnotationView will not select the MKAnnotationView. 
    • The MKMapView will not call -[MKMapViewDelegate mapView:didSelectAnnotationView:].
  • If MKAnnotationViews overlap and one is selected, touching the selected one will select an unselected one.  Since custom callouts must be implemented as ordinary MKAnnotationViews, you must disable all MKAnnotationViews behind a callout MKAnnotationView if you want the user to be able to interact with UIControls inside your callout MKAnnotationView
  • Any UIControls in your MKAnnotationView must exist within your MKAnnotationView's frame. Otherwise, when the user attempts to touch them, the map will think the user touched outside the MKAnnotationView and deselect your MKAnnotationView.
  • You may change the frame of your MKAnnotationView after the MKMapView calls -[MKMapViewDelegate mapView:viewForAnnotation:] and -[MKMapViewDelegate mapView:didAddAnnotationViews:].  However, you can't change the -[MKAnnotationView centerOffset].  So, if you change the frame size, it needs to grow equally in all directions.
    • When you don't grow the MKAnnotationView's frame evenly, it will appear to look fine at first, but when you zoom the map, it will snap to its old centerOffset.