When using a Navigation Controller in a Cocoa Touch application, the framework takes care of many things, including generation of a back button when you push a view controller to the navigation controller. The back button gets the same title of the parent view, which a lot of the time is what you might want, but every now and again it's not. For example, in my cool application, the initial view has the title 'My Cool Application' - when you navigate to the settings view, the back button has the long, unwieldy title 'My Cool Application', making the button so big that it pushes the 'Settings' title to the right.
Manually setting the title of the back button by messing with navigationItem.backBarButtonItem
doesn't work. However, you can set the leftBarButtonItem
property of the second view to a button that you create yourself, which suppresses the navigation controller from automatically creating a button. You can set the title of your button to whatever you want, but how do you get an arrow button like the automatically generated one?
I've seen some people take the approach of using an image. I prefer using an undocumented button type, 101
. Adding the code below to the constructor of my settings view controller changes the horrible big back button into a small button with the title 'Back':
- (id) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIButton* customBackButton = [UIButton buttonWithType:101];
[customBackButton setTitle:@"Back" forState:UIControlStateNormal];
[customBackButton addTarget:self
action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
myBackButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton];
[self.navigationItem setLeftBarButtonItem:myBackButton];
}
return self;
}
The myBackButton
instance variable is defined as: UIBarButtonItem* myBackButton
. The action taken when the button is pressed is defined in a method I've called done
.
Yep, that's much better!
Comments
Great posting!
I was looking for example codes for passing setting values to the original view, which would require action from the back button. If you can add that part, it'll be even more valuable & useful.
Thanks again.
I get always a rectangle button in the iphone simulator... shoud i use a real iphone?
Mark - no, it should work fine with the iPhone simulator.
Could you show us the code for the method done, to return to the parent view ?
Thx.
Your done method could look something like this:
I know this is a dated article I must say that after looking for hours this is the perfect solution to detect and control the back transition on a Navigation Controller. Thanks!.
Edgard - glad you found it useful
I've heard that this solution doesn't work for ios 6. Is that true??
Have somebody tried it?
Yunier - I have an app that uses this technique and it works fine on iOS6. The target SDK for the app is set to iOS 5 though, not sure that would make a difference. When I have a chance, I'll recompile the app with the target SDK set to iOS6 to see if it makes a difference.