如何在不增加背景图像大小的情况下增加(自定义类型)UIButton的可拍摄(击打)区域 [英] How to increase tapable (hitting) area of (custom Type) UIButton without increasing size of background image
问题描述
是否可以在不改变Button背景图像大小的情况下增加UIButton的可拍摄区域图像
is it possible to increase tapable area of UIButton without changing size of Button's background Image
我试过:
[shareButton setContentEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
&
[shareButton setImageEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
但这些都不起作用。
有什么建议吗?
推荐答案
制作类型为的UIButton buttonWithType:UIButtonTypeCustom
并为其指定一个较小尺寸的图像。
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.
例如,如果要将可点击区域设置为64x64大小,并且要显示大小为32x32的图像:按钮大小应为64x64,图像大小应为32x32。
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)];
界面生成器:
这篇关于如何在不增加背景图像大小的情况下增加(自定义类型)UIButton的可拍摄(击打)区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!