ios – Present context menu on MKAnnotationView

ios – Present context menu on MKAnnotationView

ios – Present context menu on MKAnnotationView

I want to be able to present a context menu when tapping on a MKAnnotationView directly, in addition to the callout accessory view.

So for example,

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
     let FirstAction = UIAction(title: "First Action"),
                                   handler: {_ in
          print("First action")
      })
                
      let secondAction = UIAction(title: "Second Action",
                                  handler: {_ in
          print("Second action")
      })
                
      let button = UIButton(type: .detailDisclosure)
      button.menu = UIMenu(title: "My Title"                               
                           children: [firstAction, secondAction])
      button.showsMenuAsPrimaryAction = true
      view.canShowCallout = true
      view.rightCalloutAccessoryView = button
}

Then in calloutAccessoryControlTapped I would like to perform the same action as tapping on the rightCalloutAccessoryView (UIButton) which shows the context menu (UIMenu). I thought maybe this would do the trick

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    control.sendActions(for: .primaryActionTriggered)
}

but that does not work. Perhaps I am passing the wrong Event to sendActions()? Or maybe it’s just not possible? At any rate, any assistance is much appreciated.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *