問(wèn)題描述
是否可以在不改變按鈕背景圖片大小的情況下增加 UIButton 的可點(diǎn)擊區(qū)域
is it possible to increase tapable area of UIButton without changing size of Button's background Image
我試過(guò)了:
[shareButton setContentEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
&
[shareButton setImageEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
但這些都不起作用.
有什么建議嗎?
推薦答案
制作類(lèi)型為 buttonWithType:UIButtonTypeCustom
的 UIButton 并為其分配一個(gè)較小尺寸的圖像.
Make the UIButton of type buttonWithType:UIButtonTypeCustom
and assign to it an image of a smaller size.
不要將圖片設(shè)置為背景圖片,否則它會(huì)隨按鈕一起增長(zhǎng).改為將其設(shè)置為主圖像.
Do not set the image as the background image or it'll grow with the button. Set it as the main image instead.
例如,如果您想將可點(diǎn)擊區(qū)域設(shè)置為 64x64 大小,并且想要顯示大小為 32x32 的圖像:按鈕大小應(yīng)為 64x64,圖像大小應(yīng)為 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)];
界面構(gòu)建器:
這篇關(guān)于如何在不增加背景圖像大小的情況下增加(自定義類(lèi)型)UIButton 的可點(diǎn)擊(點(diǎn)擊)區(qū)域的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!