0

I know I can set a custom color for a MKPinAnnotationView.

I can also set the title for MKAnnotationView and it's visible without the need to be tapped.

But what I want is to have an annotation with both a custom color and a visible title (as opposed to taping the MKPinAnnotationView to show its title).

Is that possible?

ispiro
  • 26,556
  • 38
  • 136
  • 291

1 Answers1

0

You need to implement the MKMAPViewDelegate and override this method below :

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

inside this method just implement something like this :

 Avoid user location pin 
        if (annotation is MKUserLocation) {
            return nil
        } else {
    // HERE YOU CONFIGURE YOUR CUSTOM MKPinAnnotationView by also leveraging dequeue for performance
            let annotationIdentifier = "AnnotationIdentifier"

// YOUR CUSTOM VIEW you want to add instead of the default one
            let nibName = "MarkerView"
            let viewFromNib = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! MarkerView
            var annotationView: MarkerView?

            // if there is a view to be dequeued, use it for the annotation
            if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MarkerView {

                if dequeuedAnnotationView.subviews.isEmpty {
// HERE YOU ADD IT AS A SUBVIEW
                    dequeuedAnnotationView.addSubview(viewFromNib)
                }
                annotationView = dequeuedAnnotationView
                annotationView?.annotation = annotation
            } else {

                let av = MarkerView(annotation: annotation as? CustomPointAnnotation, reuseIdentifier: annotationIdentifier)
                av.addSubview(viewFromNib)
                annotationView = av 
            }
    ...
    ...
 ////// ETC ETC of the configuration after regarding your annotation view 

This should answer your issue.

Mohamed.A.A
  • 344
  • 3
  • 5