如何将自定义图像添加到导航栏并保持后退方法?

时间:2012-12-04 12:44:01

标签: iphone ios uinavigationcontroller uinavigationbar

我有以下内容将自定义图像添加为后退按钮。问题是它会覆盖默认的导航控制器返回方法。

我该如何纠正?

 UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"button-back-arrow.png"] forState:UIControlStateNormal];
//[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 40, 29)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

只需添加此行和方法..

[button addTarget:self 
                       action:@selector(BtnBack_Clicked:)
             forControlEvents:UIControlEventTouchDown];

并调用此方法

-(IBAction)BtnBack_Clicked:(id)sender{

    [self.navigationController popViewControllerAnimated:YES];
}

答案 1 :(得分:0)

UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 31)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 84, 31);
[btn setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[btnView addSubview:btn];
self.navigationItem.leftBarButtonItem   = [[UIBarButtonItem alloc] initWithCustomView:btnView];
[btnView release];