問題描述
是否可以在不改變按鈕背景圖片大小的情況下增加 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 的可點擊(點擊)區域的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!