MKannotationView with UIButton as subview, button

2020-07-29 17:46发布

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.

3条回答
家丑人穷心不美
2楼-- · 2020-07-29 17:52

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.

查看更多
Juvenile、少年°
3楼-- · 2020-07-29 17:58

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.

查看更多
Luminary・发光体
4楼-- · 2020-07-29 18:00

you will have to create button action in coding like this

 [buttonInstance addTarget:self action:@selector(youraction:) forControlEvents:UIControlEventTouchUpInside];
查看更多
登录 后发表回答