Custom UIBarButtonItems from UIButtons with custom images - is it possible to make the tap targets larger?(来自带有自定义图像的 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 - 是否可以使点击目标更大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自带有自定义图像的 UIButtons 的自定义 UIBarButtonItems - 是否可以使点击目标更大?
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- UITextView 内容插图 2022-01-01
- GPS状态的广播接收器? 2022-01-01