只有 UIButton 的中心是可点击的

Only center of UIButton is clickable

我正在将自定义 UIButton 添加到 MKAnnotationView(地图上的“图钉”,而不是“气泡”),但是当我这样做时只有按钮的中心是可点击的。

这是我的按钮的方法:

- (UIButton *)button: (NSString*)text{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 100, 100)];
    [[button titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];
    text=[NSString stringWithFormat:@"%@",text];
    [button setTitle:text forState:UIControlStateNormal];
}

这里我将它添加到 MKAnnotationView:

buildingBtn = [self button:annotations.title];    
[self addSubview:buildingBtn];
buildingBtn.layer.anchorPoint = CGPointMake(1, 1);

你的 UIButton 的框架可能比 MKAnnotationView 的大。如果是这种情况,该按钮将不会接收任何位于注释视图边界之外的位置的触摸。

(重新发布问题作者的答案 material 以将其移至答案 space).

感谢@Muncken 找到了解决方案。

未设置 MKAnnotationView 的框架。通过自己设置,我可以控制可点击的大小。我还发现将 UIButton 添加到 MKAnnotationView 没有意义,因为 MKAnnotationView 充当按钮。