mpatric.com

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.

First level navigation view Second level navigation view

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.

First level navigation view Modified second level navigation view

Yep, that's much better!

Noavatar

Posted Sat 21 May 2011 by Michael Patricios

Tags: Development, iOS, Objective-C

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.

    Sean commented Fri 10 Jun 2011
  • I get always a rectangle button in the iphone simulator... shoud i use a real iphone?

    mark commented Wed 15 Jun 2011
  • 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.

    dante20007 commented Tue 15 Nov 2011
  • Your done method could look something like this:

    - (void)done {  
      [self.navigationController popViewControllerAnimated:YES];  
    }
    
  • 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 Aguirre commented Tue 29 Jan 2013
  • 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 commented Sat 31 Aug 2013
  • 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.

Post a comment