hanumanDev
hanumanDev

Reputation: 6614

How to add a custom image to a navigation bar and keep the back method in tact?

I have the following to add a custom image as a back button. The problem is that it overrides the default navigation controller back method.

How can I correct this?

 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];

thanks for any help

Upvotes: 0

Views: 262

Answers (2)

iOSDev1987
iOSDev1987

Reputation: 110

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];

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20551

just add this line and method..

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

and call this method

-(IBAction)BtnBack_Clicked:(id)sender{

    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 1

Related Questions