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.
The navigationController's backBarButtonItem is set on the item whose title you're trying to affect.
i.e. in Page 1's view controller, say, viewdidLoad:
You would not override this in Page 2.
Documentation for UINavigationItem : backBarButtonItem makes this clear:
My Logic:
Step 3 was important. I figured the cleanest way to simulate the "go back" was to just utilize UINavigationController's method (popViewControllerAnimated:). So, I just add that action to the navigationController of the viewController I'm pushing (viewControllerToPush) like so:
Example:
I never been able to create a proper
UIBarButtonItem
with custom view andsetBackBarButtonItem
.Here's the solution i found : let net
UINavigationControllerDelegate
handles everything! The trick here is to call thepopViewControllerAnimated:
method of theviewController.navigationController
so you don't have to create any custom method.Set the
backBarButtonItem
before pushing the viewController with the navigationController. Setting thebackBarButtonItem
inviewDidLoad
doesn't work.Say I have MyTableViewController. When the user taps a particular row I want to push AnotherViewController using the navigationController. The code looks something like this:
When anotherViewController is displayed the back button in the navigation bar will have
@"yourImage.png"
and the default back button style (rectangular arrow). Also note it's fine to passnil
astarget
. The button behaves like the back button.Even though is already answered this worked for me:
BTW: even in iOS4 initializing my back button with initWithCustomView: didn't work for me. ;(
I would be willing to bet that this is a bug on Apple's part as I am running into the exact same problem. The reason being is that I can get a custom
UIBarButtonItem
to appear, but only when I don't try to use theinitWithCustomView:
method. Per the API, the navigation controller checks the following things when a new view controller is pushed:My case (as well as yours) is 2. I specify code exactly the same as yours (i.e., creating a
UIButton
, setting itsimage
properties for various states, creating aUIBarButtonItem
, initializing it with theUIButton
, then setting my current view controller'sbackBarButtonItem
property to theUIBarButtonItem
); however, when I later push my view controller, nothing at all is displayed on the left-hand side of my navigation controller. Strangely, I can click where the "Back" button should be, and it pops the view controller.Interestingly, if I create a
UIBarButtonItem
using theinitWithTitle:style:target:action:
method instead of theinitWithCustomView:
method, it does show a custom button with a custom title. Also, as Travis mentioned, using theleftBarButtonItem
property instead works just fine. I'd rather adhere to the sanctioned logic, however, by specifying the "Back" button for the current view controller -- to be displayed later when a new view controller is pushed -- instead of creating a left button for the next view controller, which, arguably, should have no concern for anything pertaining to the view controller that came before it. :-\