ios – 自定义UIBarButtonItems从UIButtons与自定义图像 – 是否可以使点击目标更大?

前端之家收集整理的这篇文章主要介绍了ios – 自定义UIBarButtonItems从UIButtons与自定义图像 – 是否可以使点击目标更大?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作UIBarButton如下:
// Create "back" UIBarButtonItem
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(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];

但是,水龙头的目标很小.更准确地说,它们是自定义图像的大小. (这又是微小的)有没有办法增加他们的目标的大小?

(注意:改变UIButtons的frame属性只是拉伸图像.)

解决方法

您的代码的细微变化将会做到这一点

需要更改:

>我假设backButtonImage的大小是{28,17},并将按钮框设置为CGRectMake(0,48,37)
>删除backGroundImage并使用setImage:
>将属性imageEdgeInsets设置为UIEdgeInsetsMake(10,10,10)

你的代码将如下所示:

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0,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);

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

[toolBarItems addObject:backBarButtonItem];

您可以根据您的要求更改框架和imageEdgeInsets的值.这段代码为我工作.

猜你在找的iOS相关文章