1

I have add a view with button as a subview to MKAnnotationView

 CCBigBubleViewController* buble = [[CCBigBubleViewController alloc] init];
 [annotationView addSubView:buble.view];

It is shown perfectly, but the button does't respond to tapping.

Vitalii Boiarskyi
  • 1,363
  • 2
  • 10
  • 25
  • 1
    Try [this](http://stackoverflow.com/questions/6941199/how-to-get-click-event-from-a-button-added-over-mkannotationview) or [this](http://stackoverflow.com/questions/6862901/putting-uibutton-and-other-uicontrol-objects-inside-an-mkannotationview-and-allo). –  Jan 30 '12 at 13:29

3 Answers3

4

In your implementation of MKAnnotationView, override the hitTest method:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (CGRectContainsPoint(_button.frame, point)) {
        return _button;
    }
    return [super hitTest:point withEvent:event];
}

Your button will then receive touch events.

Jordan
  • 551
  • 3
  • 11
  • this shouldn't be necessary though... he somehow messed up his view hierarchy. (This should work though :)) – Daij-Djan May 09 '13 at 09:06
  • If you want to have a custom callout that is clickable that's the only way I found though. It's somewhat hacky but definitely not the worst thing I've done :) – Jordan May 09 '13 at 09:08
  • me neither ;) but I have a view here that has a clickable subview :) – Daij-Djan May 09 '13 at 09:59
3

your annotationView has probably not the right size. Out of it's frame subviews dont respond to touches. For testing this you can clip to bounds.

So make sure your button is inside the frame of your AnnotationView, perhaps sizeToFit will help here.

bopa
  • 1,105
  • 1
  • 12
  • 20
0

you will have to create button action in coding like this

 [buttonInstance addTarget:self action:@selector(youraction:) forControlEvents:UIControlEventTouchUpInside];
Piyush Kashyap
  • 1,965
  • 1
  • 14
  • 16