W.S W.S - 1 year ago 99
iOS Question

How to increase tapable (hitting) area of (custom Type) UIButton without increasing size of background image

is it possible to increase tapable area of UIButton without changing size of Button's background Image

I tried:

[shareButton setContentEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];


[shareButton setImageEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];

but none of these worked.

Any Suggestion please?

Answer Source

Make the UIButton of type buttonWithType:UIButtonTypeCustom and assign to it an image of a smaller size.

Do not set the image as the background image or it'll grow with the button. Set it as the main image instead.

For example if you want to set the tappable area to a 64x64 size and you want to show an image sized 32x32: the button size should be be 64x64 and the image size should be 32x32.


 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

// use an image with the desired size (for example 32x32)
[button setImage: [UIImage imageNamed: @"buttonIcon.png"] forState: UIControlStateNormal];
// just set the frame of the button (64x64)
[button setFrame: CGRectMake(xPositionOfMyButton, yPositionOfMyButton, 64, 64)];

Interface Builder:

Interface Builder example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download