I am an experienced iOS developer, but it really alarmed me. I am simultaneously reporting an Apple issue.
I am adding annotations to the MKMapKit map (Xcode 4.6). Each annotation is a MyAnnotation class; MyAnnotation defines a property, location_id, which I use to track this annotation.
The problem is simple: I want MyAnnotation with location_id of -1 to appear in front of everything else.
To do this, I override mapView: didAddAnnotationViews: in my MKMapViewDelegate:
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { // Loop through any newly added views, arranging them (z-index) for (MKAnnotationView* view in views) { // Check the location ID if([view.annotation isKindOfClass:[MyAnnotation class]] && [((MyAnnotation*)(view.annotation)).location_id intValue]==-1 ) { // -1: Bring to front [[view superview] bringSubviewToFront:view]; NSLog(@"to FRONT: %@",((MyAnnotation*)view.annotation).location_id); } else { // Something else: send to back [[view superview] sendSubviewToBack:view]; NSLog(@"to BACK: %@",((MyAnnotation*)view.annotation).location_id); } } }
it works great. I have an add button that adds annotation to a random location near the center of my map. Each time I click the add button, a new annotation appears; but nothing hides the annotation with location_id of -1.
** UNTIL ** I'm scrolling!
As soon as I start scrolling, all my annotations are reordered (z-order) and my careful styling no longer applies.
The really confusing thing is that I already did the assembly of the icons, without any problems. I created a brand new application with one view to test this problem; sending MKAnnotationView items to the back or front panel only works until you scroll through the list. There is nothing else in this skeleton app except for the code described above. I am wondering if there is any kind of bug in the latest MapKit infrastructure.
The original problem was trying to add new annotations as the user scrolls (mapView: regionDidChangeAnimated :). Added annotation; mapView: didAddAnnotationViews: code starts; and then the order is scrambled by an invisible hand within the framework (presumably when the scrolling is completed).
In case you are interested, here is my viewForAnnotation:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
And my class:
@interface MyAnnotation : NSObject <MKAnnotation> @property (strong, nonatomic, readonly) NSNumber* location_id; @property (strong, nonatomic, readonly) NSString* name; @property (strong, nonatomic, readonly) NSString* description; @property (nonatomic) CLLocationCoordinate2D coordinate; -(id) initWithID:(NSNumber*)location_id name: (NSString*) name description:(NSString*) description location:(CLLocationCoordinate2D) location; // For MKAnnotation protocol... return name and description, respectively -(NSString*)title; -(NSString*)subtitle; @end