Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow tapping anywhere on an annotation callout without a callout accessory view

Tags:

ios

mkmapview

I have a map view that adds annotations more or less like this:

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                       reuseIdentifier:@"MKPinAnnotationView"];
    annotationView.canShowCallout = YES;

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [detailButton addTarget:self
                     action:@selector(handleButtonAction)
           forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = detailButton;

    return annotationView;
}

In iOS 7, this puts an “i” icon on the right-hand side of the callout. Tapping on the icon triggers mapView:annotationView:calloutAccessoryControlTapped: (on the delegate) and handleButtonAction: (on self). I recently realized, though, that you can also tap anywhere else on the callout and the same two methods are fired.

This happens with a button of type UIButtonTypeDetailDisclosure but it doesn’t seem to happen with a UIButtonTypeCustom button. The delegate method is also not fired when I tap on the callout when there’s no accessory view at all. (That behavior isn’t surprising, of course; what’s surprising is that if the accessory view is a detail-disclosure button then these two methods are fired regardless of whether you tap on the button itself or just somewhere else in the callout.)

I’d like to get rid of the button in the callout—or at least replace it with a button showing my own image instead of the stock “i” icon—while still allowing the user to tap anywhere on the callout to trigger my action. Is this possible? I don’t see an MKMapViewDelegate method that corresponds to “callout tapped”.

like image 678
bdesham Avatar asked Mar 02 '14 20:03

bdesham


2 Answers

Try to set custom image for button without changing UIButtonTypeDetailDisclosure type.

UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];        
[detailButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];

For iOS7 and above this image will be tinted by default. If you want to keep original icon use the following

[[UIImage imageNamed:@"icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

Or if you want to remove icon at all

[detailButton setImage:[UIImage new] forState:UIControlStateNormal];
like image 152
vokilam Avatar answered Sep 23 '22 03:09

vokilam


To tap the callout button after the user has clicked on the Annotation view, add a UITapGestureRecognizer in didSelectAnnotationView. This way you can implement tap on the callout without needing the accessory views.

You can then get the annotation object back from the sender for further action.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTapped:)];
    [view addGestureRecognizer:tapGesture];
}

-(void)calloutTapped:(UITapGestureRecognizer *) sender
{
    NSLog(@"Callout was tapped");

    MKAnnotationView *view = (MKAnnotationView*)sender.view;
    id <MKAnnotation> annotation = [view annotation];
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        [self performSegueWithIdentifier:@"annotationDetailSegue" sender:annotation];
    }
}
like image 34
Dhanu A Avatar answered Sep 24 '22 03:09

Dhanu A