0

Im my MKMapView, I have a callout of an MKAnnotationView that works fine most of the time and looks like this:

enter image description here

The icons in the callout are buttons.

The problem:

In this case, hidden by the callout, there is an annotation on the map, exactly behind of a button. When I tap on this button, the AnnotationView behind the button gets the touch event. When I tap on a button without an annotation behind, the button gets the touch event.

I found this out using the accepted answer of How to debug who is eating my touches in UIKit?

How can I make sure that a touch on a button in the callout doesn't get eaten by an MKAnnotationView that is rendered behind the callout?

There is very much code and at the moment I have no idea which code is relevant to show to you.

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
  • Have you implemented [`hitTest(_:with:)`](https://developer.apple.com/documentation/uikit/uiview/1622469-hittest) in your callout view? – Rob Dec 22 '18 at 23:00
  • No, the only place where I have implemented hitTest is in a UIWindow subclass to debug: https://stackoverflow.com/questions/26109023/how-to-debug-who-is-eating-my-touches-in-uikit – Gerd Castan Dec 22 '18 at 23:03
  • If and when you update this to include code, I’d suggest creating [a much simpler, reproducible example](https://stackoverflow.com/help/mcve). We really don’t want to see the code that generates the above (because that will include a ton of unrelated kruft). We need the bare essentials only (how you created/showed your custom callout, etc.). – Rob Dec 22 '18 at 23:04
  • I'll implement that – Gerd Castan Dec 22 '18 at 23:09
  • When I’ve put buttons in custom callouts, I’ve always implemented `hitTest`. See third bullet under point 3 in [Custom `MKAnnotation` callout bubble with button](https://stackoverflow.com/a/17772487/1271826). For Swift example, see https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift. – Rob Dec 22 '18 at 23:09
  • That looks very promising, I missed the necessity of a `hitTest`. Thanks! – Gerd Castan Dec 22 '18 at 23:15
  • I don’t know if that’s the problem here (there’s really nothing here for me to go on). All I know is that I had a bear of a time capture taps properly until I did `hitTest`... – Rob Dec 22 '18 at 23:16
  • Implementing `hitTest` in Apples way doesn't help. It appears I can work around the problem by returning nil in the `hitTest` implementation of the annotationView behind the callout. That's ugly. – Gerd Castan Dec 23 '18 at 00:25
  • Hmm. When I set the hit test of my annotation view to include both the annotation view and its fallout, it worked for me. But it sounds like you figured out a solution regardless. Congrats. – Rob Dec 23 '18 at 00:37

1 Answers1

0

Found a solution (I did not find a solution using hitTest):

I now use my own UIView as callout (see Robs comment below the question as a start). That does not change my problem described in the question.

I noticed that I don't have the describd problem when I hit on the buttons in my callout view.

So I put an invisible custom button over all my non interactive labels and textViews. The infoButton shown below does nothing but catching taps. It has the same size as infoStackView below:

enter image description here

This solution should also work if annotationView.detailCalloutAccessoryView is used, as I did when I posed the question.

Gerd Castan
  • 6,275
  • 3
  • 44
  • 89