I have a UINavigationController
into which I push several views. Inside viewDidLoad
for one of these views I want to set the self.navigationItem.backBarButtonItem
to a custom view (based on a custom image). I don't know why, but it doesn't seem to work. Instead, I get the standard "back" button.
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 63, 30)];
[backButton setImage:[UIImage imageNamed:@"back_OFF.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"back_ON.png"] forState:UIControlStateSelected];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = backButtonItem;
[backButtonItem release];
[backButton release];
I tested with a standard title and it worked. What is wrong with the above code ?
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Prout" style:UIBarButtonItemStyleDone target:nil action:nil] autorelease];
Thanks for any help on this.
You can use leftBarButtonItem instead of back button item. And to remove the default back button item set it to nil like follows;
If your end goal is to simply replace the image used for the back button, you can use a new method on UIBarButtonItem available in iOS 5.0:
setBackButtonBackgroundImage:forState:barMetrics:
Apple Docs: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html
Here's a simple example that sets a custom background image for all back buttons in your app: