在导航控制器中缩小自定义后退按钮的可点击区域 [英] Make custom back button's clickable area smaller in Navigation controller

查看:24
本文介绍了在导航控制器中缩小自定义后退按钮的可点击区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码创建了一个自定义后退按钮,但是可点击区域非常大并且远远超出了图标本身.有谁知道如何设置可点击区域,或使其与图片大小相同?

I've created a custom back button with the code below, however the clickable area is very big and goes well beyond the icon itself. Does anyone know how to set the clickable area, or make it the same size as the image?

谢谢

UIImage *buttonImage = [UIImage imageNamed:@"prefs"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:buttonImage forState:UIControlStateNormal];

button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

[button addTarget:self action: @selector(handleBackButton)
    forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

self.navigationItem.leftBarButtonItem = customBarItem;

可点击区域显示为红色.

The clickable area is shown in red.

谢谢!

推荐答案

如果你想阻止除按钮以外的点击,那么将自定义按钮添加到 UIView 然后将该视图设置为 barbuttonItem 的自定义视图

If you want to prevent the click other than the button then add the custom button to UIView then set that view as custom view to the barbuttonItem

你的代码会变成这样:

UIImage *buttonImage = [UIImage imageNamed:@"prefs"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: @selector(handleBackButton)
forControlEvents:UIControlEventTouchUpInside];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[view addSubview:button];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.leftBarButtonItem = customBarItem;

这应该对我有用.

这篇关于在导航控制器中缩小自定义后退按钮的可点击区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆