简体   繁体   中英

How to make an MKAnnotationView callout with a custom button tappable?

I am trying to mimic the behavior of the MKMapView callouts in Apple's stock Maps app.

Here's what I have so far: I understand that when you add Apple's UIButtonTypeDetailDisclosure as an accessory view to the callout, the entire callout will automatically become clickable as well as the accessory. However, I don't like how Apple's detail disclosure button is a giant 'i', so I made a custom button of type UIButtonTypeCustom and added a TableViewCell as a subview so that the right accessory view is exactly like the one in the stock Maps app. It is tappable and successfully performs a segue to my detail view controller.

The problem here is that only the small accessory is tappable, and there is no visual feedback on the TouchDown event like there is in the Maps app. What I want to achieve is for the MKAnnotationView callout to be able to segue to my detail view, and have it darken the background when you TouchDown. I have searched all around here and could not find a method of doing this. Any ideas?

Here's where I customize the MKAnnotationView:

// Try to dequeue an existing pin view first.
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"OpenCustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"OpenCustomPinAnnotationView"];

            pinView.pinColor = MKPinAnnotationColorGreen;
            //pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;
            pinView.userInteractionEnabled = YES;
            UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dollarsmall"]];
            pinView.leftCalloutAccessoryView = iconView;

            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
            UITableViewCell *disclosure = [[UITableViewCell alloc] init];
            [rightButton addSubview:disclosure];
            rightButton.frame = CGRectMake(0, 0, 25, 25);
            disclosure.frame = CGRectMake(0, 0, 25, 25);
            disclosure.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            disclosure.userInteractionEnabled = NO;
            pinView.rightCalloutAccessoryView = rightButton;
        } else {
            pinView.annotation = annotation;
        }
        return pinView;

}

And here's how I handle the disclosure tapping:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"CPPMapDetailViewSegue" sender:self];
}

I was having the exact same problem you're having. I looked for hours and found nothing!

I just found this and it worked like a charm: https://stackoverflow.com/a/22322591

I only used the first two lines: creating the DetailDisclosure button and setting the image. I was able to use my custom image and it worked exactly like a standard DetailDisclosure with the "i" icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM