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 - 是否可以使点击目标更大?
基础教程推荐
- Firebase 云消息传递令牌未生成 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01