Anyone know if there’s a way to get click event from a button that is added to MKAnnotationView, this button is used as label just to display the name of each pin on the map , now I successd to show a custom view (which contains image, text ….) when the pin is clicked so i need to do the same thing when the button (label) is clicked.
Thanks for any advice you can provide.
code for button in MKAnnotationView:
UIButton * pinButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 140, 28)];
[pinButton.titleLabel setTextColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]];
[pinButton setCenter:CGPointMake(pinAnnotationView.center.x + 70, pinAnnotationView.center.y + 10)];
[pinButton addTarget:self action:@selector(pinLabelClicked) forControlEvents:UIControlEventTouchUpInside];
[pinAnnotationView addSubView:pinButton];
[pinButton setUserInteractionEnabled:YES];
The standard UI approach is to use the callout view and add an accessory button as progrmr shows.
However, if you must add a button directly to the
MKAnnotationView, the problems with your approach are that theMKPinAnnotationView‘s default frame (which can’t easily be changed) is smaller than the button you’re adding so most of the button will not respond to touches and even if you switch to using anMKAnnotationViewand increase the frame size, theMKMapViewwill prevent the button from getting any touches.What you’ll need to do is add a
UITapGestureRecognizerto the button (use the gesture handler’s action method instead of an addTarget on the button) and add the button to a plainMKAnnotationViewwith an appropriate frame size instead of anMKPinAnnotationView.Example:
Note that this will prevent the map view’s
didSelectAnnotationViewdelegate method from firing. If you need that method to fire (in addition to the button’s gesture handler method), then add the following: