Created
June 30, 2018 11:13
-
-
Save alexpaul/80d51ea637707f98abce8d061f532c93 to your computer and use it in GitHub Desktop.
Setting up a custom callout on the detailCalloutAccessoryView in a MapView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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