UIButton only responds in a small area

Posted by Tom Irving on Stack Overflow See other posts from Stack Overflow or by Tom Irving
Published on 2010-05-29T14:45:07Z Indexed on 2010/05/29 14:52 UTC
Read the original article Hit count: 278

I'm trying to add a UIButton to a UIView, but am having some trouble with getting it to respond to touches.

I have a method which returns UIButtons after I provide it with a tag:

- (UIButton*)niceSizeButtonWithTag:(int)tag {

    UIButton * aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setTag:tag];
    [aButton addTarget:self action:@selector(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];

    CGRect newFrame = aButton.frame;
    newFrame.size.width = 44;
    newFrame.size.height = 44;
    [aButton setFrame:newFrame];

    return aButton;
}

As you can see I'm creating a new button and increasing the size. I use this in the following way:

UIButton * anotherButton = [self niceSizeButtonWithTag:1];
[anotherButton setImage:[UIImage imageNamed:@"image" withExtension:@"png"] forState:UIControlStateNormal];
[anotherButton setCenter:CGPointMake(middleOfView)];
[aView addSubview:anotherButton];

I create a lot of buttons like this, hence the reason for the method.

The buttons are always created and added to the subview perfectly. I can see the image and they're in the correct position. The problem is that they only respond to touches in a tiny strip.

In this attached image,

alt text

  • The yellow shows the whole frame of the button,
  • The red shows the area that will respond to touches.
  • The grey shows a section of the view the button is added to.

If anyone could shed some light on why this is happening, it would be really useful.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c