UIKit mapView color annotations

I have tried to make colored annotations in mapView (shown in the commented sections) but they always appear in black. Any help would be appreciated.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "TempAnnotationView")
annotationView.canShowCallout = true
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
let configuration = UIImage.SymbolConfiguration(pointSize: 10, weight: .thin, scale: .default)
if annotation.title == "Start" {
// let config = UIImage.SymbolConfiguration.preferringMulticolor()
// let image = UIImage(systemName: "flag.fill", withConfiguration: config)
// // palette
// let config2 = UIImage.SymbolConfiguration(paletteColors: [.systemRed, .systemGreen, .systemBlue])
// let image2 = UIImage(systemName: "person.3.sequence.fill", withConfiguration: config2)
// // hierarchical symbols
// let config3 = UIImage.SymbolConfiguration(hierarchicalColor: .systemIndigo)
// let image3 = UIImage(systemName: "square.stack.3d.down.right.fill", withConfiguration: config3)
// // color
// let image4 = UIImage(systemName: "cone.fill")?.withTintColor(.systemRed, renderingMode: .alwaysTemplate)
// annotationView.image = image4
annotationView.image = UIImage(systemName: "poweron", withConfiguration: configuration)
}
return annotationView
}
Answered by DTS Engineer in 830333022

MKAnnotationView takes the image data from the UIImage, and uses that image data for drawing without using a UIImageView for the actual rendering of the image, so the symbol configuration is lost in that process. That's a good find, so you really should file a bug about that. Please post the FB number here once you do.

If you want to use SF Symbols, here are two paths you can use. The simplest is to use MKMarkerAnnotationView and its glyphImage property. That will respect your symbol configuration. However, if you'd prefer the look of something other than a marker annotation, you should make a custom subview of MKAnnotationView, and then you can add your own UIImageView. Our sample code project for annotations has an example of a custom annotation with an image view, so you can use that to help get you started.

— Ed Ford,  DTS Engineer

Accepted Answer

MKAnnotationView takes the image data from the UIImage, and uses that image data for drawing without using a UIImageView for the actual rendering of the image, so the symbol configuration is lost in that process. That's a good find, so you really should file a bug about that. Please post the FB number here once you do.

If you want to use SF Symbols, here are two paths you can use. The simplest is to use MKMarkerAnnotationView and its glyphImage property. That will respect your symbol configuration. However, if you'd prefer the look of something other than a marker annotation, you should make a custom subview of MKAnnotationView, and then you can add your own UIImageView. Our sample code project for annotations has an example of a custom annotation with an image view, so you can use that to help get you started.

— Ed Ford,  DTS Engineer

Thank you Ed. I have filed a bug as you suggested. MKAnnotationView loses symbol configuration during rendering - FB16957449

UIKit mapView color annotations
 
 
Q