Search code examples
iosuibuttonmkannotationview

Only center of UIButton is clickable


I'm adding a custom UIButton to an MKAnnotationView (the "pin" on the map, not the "bubble"), but when I do only the center of the button is clickable.

Here's the method for my button:

- (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];
}

Here I add it to the MKAnnotationView:

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

Solution

  • The frame of your UIButton is probably bigger than that of the MKAnnotationView. If that's the case, the button will not receive any touches with locations outside of the annotation view's bounds.