我正在制作 UIBarButtons 如下:
I'm making UIBarButtons as follows:
// Create "back" UIBarButtonItem
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 28, 17);
[backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;
UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[toolBarItems addObject:backBarButtonItem];
但是,点击目标很小.更准确地说,它们是自定义图像的大小.(同样,它们很小.)有什么方法可以增加它们的点击目标的大小吗?
However, the tap targets are tiny. More precisely, they're the size of the custom images. (Which again, are tiny.) Is there any way to increase the size of their tap target?
(注意:改变 UIButtons 的 frame 属性只会拉伸图像.)
(Note: altering the frame property of the UIButtons just stretches the image.)
对您的代码稍作改动即可解决问题
Small changes to your code will do the stuff
需要更改:
backButtonImage
的大小为 {28,17}
并将按钮框架设置为 CGRectMake(0, 0, 48, 37)
`backGroundImage
并使用 setImage:
imageEdgeInsets
设置为 UIEdgeInsetsMake(10, 10, 10, 10)
backButtonImage
is {28,17}
and setting the button frame as CGRectMake(0, 0, 48, 37)
`backGroundImage
and use setImage:
imageEdgeInsets
to UIEdgeInsetsMake(10, 10, 10, 10)
你的代码会变成这样:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 48, 37);
[backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;
UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[toolBarItems addObject:backBarButtonItem];
您可以根据需要更改 frame
和 imageEdgeInsets
的值.
这段代码对我有用.
You can change the value for the frame
and the imageEdgeInsets
as per your requirements.
This code worked for me.
这篇关于来自带有自定义图像的 UIButtons 的自定义 UIBarButtonItems - 是否可以使点击目标更大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!