Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Created June 30, 2018 11:13
Show Gist options
  • Save alexpaul/80d51ea637707f98abce8d061f532c93 to your computer and use it in GitHub Desktop.
Save alexpaul/80d51ea637707f98abce8d061f532c93 to your computer and use it in GitHub Desktop.
Setting up a custom callout on the detailCalloutAccessoryView in a MapView
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "PlaceAnnotationView") as? MKMarkerAnnotationView
if annotationView == nil {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "PlaceAnnotationView")
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
// initialize your custom view
let customCalloutView = CustomCalloutView()
customCalloutView.delegate = self
// setup constraints for custom view
customCalloutView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
customCalloutView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.60),
customCalloutView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height * 0.30)
])
// set detailCalloutAccessoryView
annotationView?.detailCalloutAccessoryView = customCalloutView
return annotationView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment