Replacing a calloutaccessoryview in mkannotationview
In my viewForAnnotation
delegate, I create a MKAnnotationView
with leftCalloutaccessory
as a info button. When the info button is tapped, I want to replace it with a UIActivityIndicator
. So in the calloutAccessoryTapped
delegate, I wrote
view.leftCalloutaccessoryView = [UIActivityIndicator indicatorWithStyle:UIActivityIndicatorWhite];
The callout accessory seems to change, but it seems like the view doesn't get updated immediately.
That's when the callout accessory gets hidden (by tapping another pi开发者_JAVA百科n) and is re-opened, I see a spinner. But otherwise, I don't.
I tried calling [view setNeedsdisplay]
and [view setNeedsLayout]
and [view layoutsubviews]
as well but in vain.
Any suggestions/pointers?
I would consider removing and re-adding your annotation. It seems a little heavy handed, but might work.
I was having a similar problem -- I needed to display an image for the leftCalloutAccessoryView if there was one associated with the annotation. Try setting it in mapView:didSelectAnnotationView instead of in mapView:viewForAnnotation
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotView
{
...
if(imgURL.length>1){
// set a thread that will load the image
// but don't set the leftCalloutAccessoryView until the image is loaded
dispatch_queue_t downloader = dispatch_queue_create("annotation image downloader",NULL);
dispatch_async(downloader, ^{
NSURL* photoURL = [NSURL URLWithString:imgURL];
NSData* photoData = [NSData dataWithContentsOfURL:photoURL];
UIImage* photoImage = [UIImage imageWithData:photoData];
dispatch_async(dispatch_get_main_queue(), ^{
annotView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
[(UIImageView *) annotView.leftCalloutAccessoryView setImage:photoImage];
});
});
}
}
精彩评论