Show callout for disclosureindicator for MKAnnotationView in Swift

132 Views Asked by At

I am able to show a disclosure icon in on my map annotation using the following code.

  annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

When you press the disclosure icon, it blinks but nothing else happens. I tried putting a button handler on it but it is not a button so the compiler said no. Do I have to create a whole gesturerecognizer to get it to do anything or how would I get a press on the the indicator to show information about the location?

2

There are 2 best solutions below

1
Shehata Gamal On BEST ANSWER

You need this delegate method

func mapView(_ mapView: MKMapView, 
   annotationView view: MKAnnotationView, 
   calloutAccessoryControlTapped control: UIControl)

with

self.mapView.delegate = self
1
Satish On

Try this

   let btn = UIButton(type: .detailDisclosure)
   btn.addTarget(self, action: #selector(btnDisclosureAction(_:)), for: .touchUpInside)
   annotationView.rightCalloutAccessoryView = btn

    @objc func btnDisclosureAction(_ sender: UIButton) {
        // you action
    }