本文介绍了来自带有自定义图像的 UIButtons 的自定义 UIBarButtonItems - 是否可以使点击目标更大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作 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)
- I am assuming that the size of
backButtonImage
is{28,17}
and setting the button frame asCGRectMake(0, 0, 48, 37)
` - remove the
backGroundImage
and usesetImage:
- set the property
imageEdgeInsets
toUIEdgeInsetsMake(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 - 是否可以使点击目标更大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!