UIBarButtonItem UINavigationBar中的自定义视图

我正在为uinavigationbar自定义栏button,我正在使用以下代码

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; 

问题是没有调用它的行为。 我做错了什么?

从Apple文档:
使用指定的自定义视图初始化新项目。

 - (id)initWithCustomView:(UIView *)customView 

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

讨论: 通过此方法创build的栏button项目不会响应用户交互调用其目标的操作方法。 相反,栏button项目期望指定的自定义视图来处理任何用户交互并提供适当的响应。

解决scheme:“使用背景图像创buildbutton(将动作设置为该button)以及使用此button的init barbutton”。 例如:

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0,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]; 
 UIBarButtonItem *graphButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"GraphButton.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(graphButton)]; self.navigationItem.rightBarButtonItem = graphButton; - (void)graphButton{ } 

我用下面的代码完成了这个:

 UIButton *nxtBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [nxtBtn setImage:[UIImage imageNamed:@"chk_next.png"] forState:UIControlStateNormal]; [nxtBtn addTarget:self action:@selector(NextBtn) forControlEvents:UIControlEventTouchUpInside]; [nxtBtn setFrame:CGRectMake(0, 0, 64, 31)]; UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithCustomView:nxtBtn]; checkIn_NavBar.topItem.rightBarButtonItem=nextButton;