MKannotationView with UIButton as subview, button don't respond

空扰寡人 提交于 2019-12-12 09:15:08

问题


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.


回答1:


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.




回答2:


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.




回答3:


you will have to create button action in coding like this

 [buttonInstance addTarget:self action:@selector(youraction:) forControlEvents:UIControlEventTouchUpInside];


来源:https://stackoverflow.com/questions/9064348/mkannotationview-with-uibutton-as-subview-button-dont-respond

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!