1

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.

Peter Jacobs
  • 1,657
  • 2
  • 13
  • 29

1 Answers1

-1

you can use changesSelectionAsPrimaryAction api available from iOS 15 to display menu as primary action for button selection, add for your button

button.changesSelectionAsPrimaryAction = true
Jayesh Patel
  • 938
  • 5
  • 16
  • Thanks for your response, but that alone did not work. Do I need to make other coding changes above, particularly in `func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)`? – Peter Jacobs Oct 29 '21 at 02:50