ios – UIBarButtonItem UINavigationBar中的自定义视图

我正在为uinavigationbar制定一个自定义栏按钮,我正在使用以下代码
UIImageView *backImgView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chk_back.png"]]; [backImgView setUserInteractionEnabled:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backImgView];
[backButton setTarget:self];
[backButton setAction:@selector(BackBtn)];
checkIn_NavBar.topItem.leftBarButtonItem = backButton;

问题是它不是调用它的动作.我做错了什么?

解决方法

从苹果文档:
使用指定的自定义视图初始化新项目.
- (id)initWithCustomView:(UIView *)customView

参数
customView
表示项目的自定义视图.
回报值
具有指定属性的新初始化项.

讨论:
此方法创建的条形按钮项目不会响应于用户交互调用其目标的操作方法.相反,条形按钮项目期望指定的自定义视图来处理任何用户交互并提供适当的响应.

解决方案:“使用背景图像创建按钮(将操作设置为此按钮)和使用​​此按钮的初始栏按钮”.例如:

UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,25,25)
[btn setBackgroundImage:[UIImage imageNamed:@"chk_back.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(BackBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...