3

I've tried the two methods found in these stackoverflow posts without luck: post a, post b.

The methods are, a, to bring the view to front from the superview, b, to set the z postion of the layer of the view.

Dose anyone know a third way?

Heestand XYZ
  • 1,993
  • 3
  • 19
  • 40
  • What you need to achieve? – Reinier Melian Jun 26 '17 at 20:41
  • Control the order MKAnnoationViews are overlapping each other in my MKMapView. – Heestand XYZ Jun 26 '17 at 22:13
  • I've been thinking. How can both of these methods work (in theory), would not one override the other? Dose z-position override the subview order? – Heestand XYZ Jun 27 '17 at 15:35
  • I think not, z-position is layer property, one view can have any number of layers, – Reinier Melian Jun 27 '17 at 16:25
  • After some research I found out that the layer zPosition actually overrides the subview order. – Heestand XYZ Jun 28 '17 at 18:35
  • Seems like MKMapView got some auto ordering going on, so the lower pins appear above, dose anyone know how to disable it? – Heestand XYZ Jun 29 '17 at 21:29
  • Is there a MKOverlayLevel for MKAnnotations? – Heestand XYZ Jun 29 '17 at 22:21
  • 2
    As you said @AntonHeestand, it seems that `MKMapView` do its special sauce when rendering annotation views. I tried both of the methods (subview order / zposition), both have no effect on iOS11. I still haven't found any solution to this problem. Have you? Or do I have to tell my client that is impossible to develop something as simple as showing the first annotation over the second?! What a shame... – Martin Oct 26 '17 at 15:53
  • @Martin Unfortunately I never found a solution. – Heestand XYZ Oct 30 '17 at 11:00
  • I'm in the same boat, and agree that neither of the previous approaches work on iOS 11. I also tried reordering the adding of annotations, but the resulting order doesn't even reflect that. I can't see (from incidental cursory monitoring) any pattern to the ordering the MKMapView decides to give them. – theLastNightTrain Nov 07 '17 at 15:56
  • 1
    An iOS 11 solution can be found here: https://stackoverflow.com/a/48453515/320737 – James H Jan 25 '18 at 23:25

1 Answers1

4

Try setting anchorPointZ on the MKAnnotationView's layer. Keep in mind that higher values place the view lower in the stacking order.

annotationView.layer.anchorPointZ = 0

This works on macOS 10.13. I haven't tested yet on iOS 11, but MapKit is essentially identical on both platforms.

ChrisJF
  • 6,822
  • 4
  • 36
  • 41
Ryan Holmes
  • 271
  • 4
  • 5
  • At first this solution was working for me, but after a bunch of tapping on my MKAnnotationViews, it's not a reliable solution :( – ChrisJF Nov 16 '18 at 04:57
  • Did you reset the other annotations' anchorPointZ back to a value > 0? If not, then all annotations may end up with a z-postion of 0 which will revert back to the original problem. – Michael Kurtz Sep 19 '20 at 20:31