July 18
Custom UINavigationController Back Button
A common thing in client apps i write is custom back images, mostly because the apps have a custom UINavigationBar image, so its good to have them themed together. You can see what this looks like for this tutorial:
![]()
Ok so in your View Controller, thats shown after something in your navigation view is shown, add this code in the viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"back_button.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// Cleanup
[customBarItem release];
}
Here we load the image we want for the back button in the navigation bar, create a custom UIButton and set the image to the button and then specify that the button is the left button item. I also add a selector to catch clicks, so we know when the user presses back. It calls the back method which quite simply:
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
And thats it! You can get the project source here – CustomBackButton



Perfect! Thank you for posting this code; works as advertised and solves my problem.
Thanks a lot for this snippet!
Just a very small thing, ‘customBarItem’ needs to be released to avoid memory leak.
Thanks Peter, yes of course every-time you call ‘alloc’ you need to have a corresponding release/autorelease if the object is getting retained somewhere else.
Thanks Jeff,
what about the navigation bar background image ?
Raed, Perhaps look at this thread
Thanks for the tutorial! i was looking forever for something like this
Did not work in the viewDidLoad method, but did in the view controller’s init method. It’s all good.
thanks Jeff!
twitter how to get followers…
[...]Custom UINavigationController Back Button | Applausible Blog[...]…
Thank you very much for this Tutorial! You helped me out a lot!
Perfect! it is very useful tutorial :)
thank you guy!
It almost works like a back button. With the exception of the transition animation (after the button is tapped and current view is popped off the navigation to the right).
Legitimate back button animation – fade out and move to the right (at the same speed as title label moves to the right)
Fake back button animation – fade out (no movement).
Once you notice it once, this inconsistency becomes quite annoying.
Perfect :-)
You have heard about viral marketing, right? I discovered some software that can automate the whole process. It takes the very best of all automated viral marketing strategies (and then some) and packages them into one, stupidity simple to use push button software that harnesses the power or word of mouth marketing. This really is some entirely new technology. Go right on ahead and check it out.. http://tinyurl.com/6sfb37g
where does the back method go?
Thanks posting this example!
Hi there, I discovered your web site via Google while searching for a related topic, your web site got here up, it looks great. I’ve added to favourites|added to my bookmarks.
To fix Legitimate back button animation:
add [button setImage:buttonImage forState:UIControlStateHighlighted]; in viewDidLoad