Z-ordering of MKAnnotationViews

半世苍凉 提交于 2019-11-27 20:44:13

If you have a custom annotationView (which sounds like you already have) you can try adding this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self.superview bringSubviewToFront:self];
    [super touchesBegan:touches withEvent:event];
}

A side-effect is that each time a marker is touched it also pops to the front of the z-order, which actually is sensible since that's what the user is focusing on.

Ramin, that is perfect for what I am trying to do. I did not have a custom annotation view defined though. I solved this by using a category and it worked perfect!

Here is the header:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h>


@interface MKPinAnnotationView (ZIndexFix)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
@end

And the Implementation:

#import "AEBMapViewCategory.h"
@implementation MKPinAnnotationView (ZIndexFix)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self.superview bringSubviewToFront:self];
    [super touchesBegan:touches withEvent:event];
}
@end

Try another solution, setup annotation view layer's zPosition (annotationView.layer.zPosition) in:

(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!