Customizing MKAnnotationView only through subclassed MKMapView?
I've tried both this (MKMapView Delegate):
-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *annotationView in views)
{
annotationView.image = [UIImage imageNamed:@"itemType2.png"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.canShowCallout = YES;
}
}
and this (on the VC that has the MKMapView):
[self.theMapView addAnnotations:annotationsArray];
for (id <MKAnnotation> itemAnnotation in self.theMapView.annotations)
{
MKAnnotationView *itemAnnotationView = [self.theMapView viewForAnnotation:itemAnnotation];
itemAnnotationView.image = [UIImage imageNamed:@"itemType2.png"];
itemAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
itemAnnotationView.canShowCallout = YES;
//[self.theMapView setNeedsDisplay];
}
whithout any success on changing the MKAnnotationView's appearance, they appear as si开发者_高级运维mple red pins without the disclosure buttons or anything....
Is the only way of changing them through creating a subclassed MKMapView and using - (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation ??? I feel it's desnecessary to create an extra subclass just for changing the annotations, why don't the above methods work?
You don't need to subclass anything to set those properties on an MKAnnotationView
.
You need to implement the viewForAnnotation
delegate method and set the properties there.
See this answer for a code example.
The only change from that example is to use MKAnnotationView
instead of MKPinAnnotationView
.
精彩评论