Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

uibutton

ios5

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?

like image 604
W.S Avatar asked Nov 06 '12 07:11

W.S


1 Answers

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.

Programmatically:

 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

like image 97
2 revs, 2 users 84% Avatar answered Nov 15 '22 13:11

2 revs, 2 users 84%