Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The standard UI approach is to use the callout view and add an accessory button as progrmr shows.</p> <p>However, if you must add a button directly to the <code>MKAnnotationView</code>, the problems with your approach are that the <code>MKPinAnnotationView</code>'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 an <code>MKAnnotationView</code> and increase the frame size, the <code>MKMapView</code> will prevent the button from getting any touches.</p> <p>What you'll need to do is add a <code>UITapGestureRecognizer</code> to the button (use the gesture handler's action method instead of an addTarget on the button) and add the button to a plain <code>MKAnnotationView</code> with an appropriate frame size instead of an <code>MKPinAnnotationView</code>.</p> <p>Example:</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id&lt;MKAnnotation&gt;)annotation { MKAnnotationView *annView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: @"pin"]; if (annView == nil) { annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"] autorelease]; annView.frame = CGRectMake(0, 0, 200, 50); UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; pinButton.frame = CGRectMake(0, 0, 140, 28); pinButton.tag = 10; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinButtonTap:)]; tap.numberOfTapsRequired = 1; [pinButton addGestureRecognizer:tap]; [tap release]; [annView addSubview:pinButton]; } annView.annotation = annotation; UIButton *pb = (UIButton *)[annView viewWithTag:10]; [pb setTitle:annotation.title forState:UIControlStateNormal]; return annView; } - (void) handlePinButtonTap:(UITapGestureRecognizer *)gestureRecognizer { UIButton *btn = (UIButton *) gestureRecognizer.view; MKAnnotationView *av = (MKAnnotationView *)[btn superview]; id&lt;MKAnnotation&gt; ann = av.annotation; NSLog(@"handlePinButtonTap: ann.title=%@", ann.title); } </code></pre> <p><br> Note that this will prevent the map view's <code>didSelectAnnotationView</code> delegate method from firing. If you need that method to fire (in <em>addition</em> to the button's gesture handler method), then add the following:</p> <pre><code>//in the view controller's interface: @interface YourVC : UIViewController &lt;UIGestureRecognizerDelegate&gt; //where the UITapGestureRecognizer is created: tap.delegate = self; - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer :(UIGestureRecognizer *)otherGestureRecognizer { return YES; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload