Linux world
Linux world

Reputation: 3768

How to set an image to UINavigationController's back button

I want to set an image to the back button of UINavigationController

Upvotes: 1

Views: 2757

Answers (3)

Oleh Kudinov
Oleh Kudinov

Reputation: 2543

Use this code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,36,30);
[button setBackgroundImage:[UIImage imageNamed:@"backgroundImage.png"] forState:UIControlStateNormal];

[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];

Or use this library. Very easy and good looking result.

https://github.com/boctor/idev-recipes/tree/master/CustomBackButton

Upvotes: 4

iwasrobbed
iwasrobbed

Reputation: 46713

You cannot change the default back button since it will not let you override it. Instead you have to use the leftBarButtonItem to create a custom "back" button.

More information here: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25

Some prior code to reference: UINavigation controller button like backButton

Upvotes: 0

Lou Franco
Lou Franco

Reputation: 89232

Just call popViewControllerAnimated on the navigation controller and it will go back.

Upvotes: -2

Related Questions