150

The "back button" of a UINavigationController by default shows the title of the last view in the stack. Is there a way to have custom text in the back button instead?

1
  • 1
    This work for me self.navigationController.navigationBar.topItem.title = @"Custom text";
    – huynguyen
    Commented Aug 31, 2017 at 6:04

16 Answers 16

345

From this link:

self.navigationItem.backBarButtonItem =
   [[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
            style:UIBarButtonItemStylePlain
           target:nil
           action:nil];

As Tyler said in the comments:

don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button

16
  • 4
    You should release the UIBarButtonItem. Commented Sep 17, 2009 at 22:53
  • 218
    Also note, you need to do this in the view controller one level up the stack. In other words, don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button.
    – Tyler
    Commented Sep 17, 2009 at 23:07
  • 6
    Just to clarify Tyler's comment because I did not understand this at first: Setting the backBarButtonItem in a view controllers navigationItem is setting the button that will show to get BACK to this view controller. Also note that although there is no button style for the arrow button this button will get the arrow style anyway. I have used UIBarButtonItemStylePlain and still get the arrow button style. Commented Jul 9, 2011 at 15:51
  • 3
    Suggestion: Rather than assume the style, pass the currently set style. In this answer's code, replace UIBarButtonItemStyleBordered with a call to the property self.navigationItem.backBarButtonItem.style. Documentation: developer.apple.com/library/ios/documentation/uikit/reference/… Commented Aug 26, 2013 at 5:12
  • 1
    Note that under ARC you must leave out the autorelease call. so you have self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Custom Title" style:UIBarButtonItemStyleBordered target:nil action:nil]; Commented Feb 21, 2014 at 22:56
44

You can set the text in the Interface Builder:

Select the navigation item of the ViewController that the back button would return to:

enter image description here

In the utilities panel attribute inspector, enter your label for the Back Button:

enter image description here

I would prefer this approach over setting the title in code as in the accepted answer.

Also note, you need to do this in the view controller one level up the stack. In other words, don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button.
--Tyler

6
  • This is the best answer I found so far. Commented Mar 8, 2014 at 21:15
  • 2
    To be clear, this needs to be set on the view controller's Navigation Item, not on the view controller's attributes inspector itself. Commented Apr 21, 2014 at 14:39
  • This is the best answer I found.
    – bicepjai
    Commented Apr 27, 2014 at 1:13
  • This is the best answer
    – Alex Brown
    Commented Dec 3, 2014 at 21:25
  • 2
    As of Xcode 6.1.1, and running in iOS 7, it seems your original previous view title will STILL override whatever back button title you set here. If the previous view's title contains more than the limit of 11 chars, then the next view's back button will simply say "Back". There's no way around it except to create a new nav bar item from scratch as shown in the top-voted answer here.
    – ray
    Commented Dec 31, 2014 at 19:48
22

I use this:

// In the current view controller, not the one that is one level up in the stack
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backItem.title = @"Custom text";
}
3
  • This almost works for me, but when I click the back button, the text changes back to the title of the parent for the duration of the animation :-( Commented Jun 6, 2010 at 15:25
  • 3
    This changes the Back button title of the controller up in the stack. It does not change the Back button title of the currently displayed controller. Even worse: it changes the title of the controller previous to the previous controller!
    – leviathan
    Commented Jul 5, 2010 at 14:05
  • As noted by other commenters, this method is a little untenable. I wouldn't recommend doing this as it's not really how the API intends you to use it. You could be setting yourself up for difficult bugs in the future!
    – RickDT
    Commented May 3, 2012 at 14:42
11

I found a handy solution to this by simply setting the title of the controller before pushing another controller onto the stack, like this:

self.navigationItem.title = @"Replacement Title";
[self.navigationController pushViewController:newCtrl animated:YES];

Then, make sure to set the original title in viewWillAppear, like this:

-(void)viewWillAppear:(BOOL)animated
{
  ...
  self.navigationItem.title = @"Original Title";
  ...
}

This works because the default behavior of UINavigationController when constructing the back button during a push operation is to use the title from the previous controller.

3
  • 1
    The problem with this is the title changes on the old view as it animates out and looks a bit naff. None of the given solutions here seem to work as you'd like :-( Commented Jun 6, 2010 at 15:28
  • 1
    This is perfect in my situation because I don't show the NavBar on the previous screen and I just want to remove the titleLabel from the back button on the new screen and have just the iOS7 chevron arrow. I can therefore just set an empty NSString literal for the title.
    – siburb
    Commented Oct 31, 2013 at 10:54
  • pushing view controller raise the exception when tap on back
    – Sumit
    Commented Dec 15, 2016 at 11:18
7

The title of the back button defaults to the previous view's title so a quick trick I use is to place the following code on the previous view's .m file.

-(void)viewWillAppear:(BOOL)animated {

    // Set title
    self.navigationItem.title=@"Original Title";
}

-(void)viewWillDisappear:(BOOL)animated {

    // Set title
    self.navigationItem.title=@"Back";
}
4
  • This works pretty well for many cases and it's pretty simple... only observation is that overriding either of these methods requires that the super's implementation be called at some point in the new method.
    – izk
    Commented Dec 19, 2012 at 1:31
  • 2
    With this method, one will notice the title of the parent view being changed to 'Back' before the push animation happens. (And, sorta sucks, that if you move this to the viewDidDisappear method, you'll see the switch happening after the push..) Commented Jan 15, 2013 at 12:40
  • I love this method but the problem I face with this is that the "Original Title" gets set alright but it comes with an ellipsis if it is longer than "Back" which must be because, since the view is not yet in the view hierarchy, it does not relayout the title view. The problem described by @JamesBoutcher above is unnoticeable for me due to the fast initial speed of the animation. In fact, I just realized that the problem I face is especially noticeable due to slow ending animation which is when this problem is prominent.
    – trss
    Commented Jan 24, 2014 at 17:13
  • While the answer at stackoverflow.com/questions/1441699/… feels like more customization than necessary, it is the documented way and more importantly seems to be more aligned to our intent.
    – trss
    Commented Jan 24, 2014 at 17:44
6

in your init method, add the following code:

- (id)initWithStyle:(UITableViewStyle)style {
    if(self = [super init]) {

        //...

        UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                             style:UIBarButtonItemStylePlain 
                                            target:self 
                                            action:@selector(goBack)];
        self.navigationItem.leftBarButtonItem = customBackButton;
        [customBackButton release];

        //...
    }
    return self;
}

then add a simple method, to allow viewcontroller dismissing:

-(void)goBack {
    [self.navigationController popViewControllerAnimated:YES];  
}
3

Add the following code in viewDidLoad or loadView

self.navigationController.navigationBar.topItem.title = @"Custom text";

I tested it in iPhone and iPad with iOS 9

2
  • Should be self.navigationController?.navigationBar.backItem?.title = "Custom Text"
    – Peacemoon
    Commented Jun 2, 2016 at 13:03
  • 1
    only difference I needed was the optional chaining on the navigationController and topItem properties, e.g.: self.navigationController?.navigationBar.topItem?.title = "Custom Text"
    – Bulwinkel
    Commented Jun 8, 2016 at 14:42
2

Adding to rein's answer. Note from Apple's docs that the declaration of backBarButtonItem is this:

@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem

Therefore, rein's answer will leak memory because the synthesized setter will retain the instance you pass it, which is never released explicitly. You can remedy this by using autorelease

 self.navigationItem.backBarButtonItem = 
      [[[UIBarButtonItem alloc] initWithTitle:@"Custom Title" 
         style:UIBarButtonItemStyleBordered
         target:nil
         action:nil] autorelease];  //<-- autoreleased

Or you could point a variable at the instance so you can explicitly release it later:

UIBarButtonItem* item = ...
self.navigationItem.backBarButtonItem = item;
[item release];

Hope this helps!

1
  • Note these days, with ARC, you don't need to worry about the autorelease (and indeed the compiler will error if you try to autorelease in an ARC-enabled project)
    – Eric G
    Commented Aug 22, 2012 at 23:58
2
- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
  self.navigationItem.backBarButtonItem = backButton;
  [backButton release];
}
2

I've discovered something interesting. If you subclass the UINavigationController and override the pushViewController:animated: method and do something like this: (bear in mind that I'm using ARC)

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
 initWithTitle: @"Back" 
 style: UIBarButtonItemStyleBordered
 target: nil action: nil];

viewController.navigationItem.backBarButtonItem = backButton;

[super pushViewController:viewController animated:animated];

Then for all ViewControllers that are pushed with your navigation controller will have the "Back" button in them automatically. If you want to change the text for certain view controllers you can try and maybe cast the viewcontroller to a certain class or your own custom protocol (which your viewcontroller inherits from which could have a method like backButtonText or something silly like that) which can give you certain information on the viewcontroller that's coming in sothat you can customize the back button text for it. Now the back button text is taken care of in a place which should hold the responsibility solely. I have to admit that creating a new button to change the text sucks, but oh well.

Can anyone think of a reason why not to do it like this? Atleast you don't have to fiddle with viewcontroller titles or have to remember to create a new back button before pushing the viewcontroller on the navigation controller.

0

rein's answer works well.

Note that if you push more than one view controller, the changed back button title will appear for each of them, which may not be what you want.

In that case, you'll need to create the custom UIBarButtonItem each time you push a view controller.

Also, make sure you do it before pushing the view controller, otherwise you will get a screen hiccup as the title changes.

0

Expanding on Aubrey's suggestion, you can do this in the child view controller:

create two variables for storing the old values of the parent's navigationItem.title and the parent's navigationItem

UINavigationItem* oldItem;
NSString* oldTitle;

in viewDidLoad, add the following:

oldItem = self.navigationController.navigationBar.topItem;  
oldTitle = oldItem.title;  
[oldItem setTitle: @"Back"];  

in viewWillDisappear, add the following:

[oldItem setTitle: oldTitle];  
oldTitle = nil;  // do this if you have retained oldTitle
oldItem = nil;   // do this if you have retained oldItem

It's not perfect. You will see the the title of the parent view change as the new controller is animated in. BUT this does achieve the goal of custom labeling the back button and keeping it shaped like a standard back button.

0

Put this into you viewDidLoad, hope it will result into what you are looking for

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" 
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
0

if You want to set title in ARRIVING controller (sometimes more logic..) in swift 3 do:

func setBackButtonNavBar(title: String, delay: Double){

    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: { () -> Void in

        if let navBar = self.navigationController?.navigationBar{
            navBar.backItem?.title = title
        }
    })

}

in upcoming controller:

override func viewDidLoad() {
    super.viewDidLoad()
    self.setBackButtonNavBar(title: "back", delay: 0.3)
}

usually I put self.setBackButtonNavBar in a controller extension.

0

I know this is an old question and the answers' kind of out updated!

The easy way is to do this in parent ViewController:

i.e the one that takes you to next view controller.

self.navigationItem.backBarButtonItem  = UIBarButtonItem(title: "Custom text here", style: .plain, target: nil, action: nil)
-1

Doing this in code remove the back button style of the UINavigationConroller. If you add a Navigation Item in each of yours views, you can set the title of the back botton in the StoryBoard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.