RightCalloutAccessoryView not displayed in MKPinAnnotationView

I use MKPinAnnotationView in MKMapView, the title and subtitles are displayed fine , but rightCalloutAccessoryView not displayed.

I tried google many times but haven’t been able to show it yet. My code is below.

 - (void)addAnnotation { CLLocationCoordinate2D theCoordinate = self.location.coordinate; MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:theCoordinate]; annotation.title = @"Pin"; annotation.subtitle = @"More information"; [self.mapView addAnnotation:annotation]; } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MapAnnotation class]]) { static NSString *reuseIdentifier = @"MyMKPinAnnotationView"; MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier]; if (!annotationView) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; annotationView.pinColor = MKPinAnnotationColorRed; annotationView.enabled = YES; annotationView.canShowCallout = YES; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; } else { annotationView.annotation = annotation; } } return nil; } 

Please note that the title and subtitles may be displayed.

Thanks in advance.

+4
source share
1 answer

Title and subtitle viewing is the default behavior for the annotation view. If you want to do something else (for example, rightCalloutAccessorView ), you must make sure your viewForAnnotation is called by setting the delegate your MKMapView , and also make sure your viewForAnnotation returns the annotationView that you create.

One of two problems arises:

  • Your viewForAnnotation returns nil in all cases. Make sure you return annotationView after creating / modifying it.

  • If your delegate map view is not set, your current viewForAnnotation will not be called at all.

    Make sure you have installed delegate . You can do this programmatically:

     self.mapView.delegate = self; 

    Or you can do this in IB (by selecting the connection inspector, the right-most tab of the right panel and control - format from delegate to the status bar of the scene):

    enter image description here

    If so, if you put an NSLog statement or breakpoint in your viewForAnnotation . You may not see that it is called at all if the delegate not configured correctly.

+7
source

Source: https://habr.com/ru/post/1482837/


All Articles