Jane Doe Jane Doe - 1 month ago 8
Swift Question

Annotation title not appearing when adding viewForAnnotation

I have added this:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}

let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")

if let title = annotation.title{
if title! == "P"{
pinView.pinTintColor = UIColor.yellow
}
else{
pinView.pinTintColor = UIColor.gray
}
}
return pinView
}


But when I do this I can´t see my titles when I click on an annotation anymore, any ideas why?

Answer

You need to add the canShowCallout on your pinView. If the value of the canShowCallout property is true, a standard callout bubble is shown when the user taps a selected annotation view. The callout uses the title and subtitle text from the associated annotation object.

So just add the following row to your viewFor function and you´ll see the title.

pinView.canShowCallout = true
Comments