Search code examples
objective-cmkannotationmapkit

Touchable image as annotation callout?


I tried to use BarButton but this is wrong:

CGSize thumbSize = CGSizeMake(30.0, 30.0);
UIImageView *annotationThumbnail = [[UIImageView alloc] initWithImage:[((Sight *)annotation).thumbnail imageToFitSize:thumbSize method:MGImageResizeScale]];
UIBarButtonItem *annotationButton = [[UIBarButtonItem alloc] initWithCustomView:annotationThumbnail];

annotationView.leftCalloutAccessoryView = annotationButton;

Solution

  • UIBarButtonItem is not a subclass of UIView, so using an instance of UIBarButtonItem in a place that expects a view isn't going to work. Instead, you could just use annotationThumbnail like this:

    annotationView.leftCalloutAccessoryView = annotationThumbnail;
    

    If you instead use a UIControl subclass, like UIButton, map's delegate will get a -mapView:annotationView:calloutAccessoryControlTapped: message if someone taps the callout accessory:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setBackgroundImage:someImage forState:UIControlStateNormal];
    button.frame = someRect;
    annotationView.leftCalloutAccessoryView = button;