问题描述
是否可以在不改变按钮背景图片大小的情况下增加 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)];
但这些都不起作用.
有什么建议吗?
推荐答案
制作类型为 buttonWithType:UIButtonTypeCustom
的 UIButton 并为其分配一个较小尺寸的图像.
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 的可点击(点击)区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!