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.
Best Solution
As of iOS5 we have an excellent new way of customizing the appearance of almost any control using the UIAppearance protocol, i.e.
[UIBarButtonItem appearance]
. The appearance proxy allows you to create application wide changes to the look of controls. Below is an example of a custom back button created with the appearance proxy.Use the example code below to create a back button with custom images for normal and highlighted states. Call the following method from you appDelegate's
application:didFinishLaunchingWithOptions:
This is just a quick example. Normally you would want to have separate images for normal and highlighted (pressed) state.
If you are interested in customizing the appearance of other controls, some good examples can be found here: http://ios.biomsoft.com/2011/10/13/user-interface-customization-in-ios-5/