90

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.

I tried the following which didn't work:

self.navigationItem.leftBarButtonItem.title = @"Back";

Thanks.

TheNeil
  • 3,321
  • 2
  • 27
  • 52
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
  • 3
    Bear in mind that back buttons have titles for a reason. By changing the title to "Back" you're robbing the user of information about what the button goes back to. – Simon Jul 01 '13 at 19:09

20 Answers20

167

Try

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

I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationItem.

Pang
  • 9,564
  • 146
  • 81
  • 122
Marc W
  • 19,083
  • 4
  • 59
  • 71
  • 1
    Sorry, I don't have a chance to test this right now, but does this apply the left-pointed shape of the built-in Back buttons, or does it keep the UIBarButtonItem's default rectangular shape? – Marco May 07 '09 at 17:32
  • I'm actually not sure. I assume that it would just keep the back button shape automatically since you're setting the backButton property, but you'd have to test it to find out. I'm curious, too. – Marc W May 07 '09 at 19:32
  • 2
    Yes, it worked! It yes changes the title, and yes, saves the shape. Thanks for helping. – Ilya Suzdalnitski May 07 '09 at 19:43
  • 1
    Note: the back button is retained twice [1x from alloc, 1x from the property's retain]. However, if you don't change the back button, it's effectively harmless. – Kelvin Jul 30 '09 at 01:17
  • 29
    See Marco's comment below - you should call this on the controller that's about to push something new, not on the controller getting pushed. – Bill May 26 '11 at 22:42
  • @Kelvin The button is being autoreleased, so it should be fine. – raidfive Nov 25 '11 at 20:19
  • Although this has been alluded to on this page, it wasn't explicitly said and took me a couple minutes of scratching my head. It's best to place the above code in the viewWillDisappear method of the class that's pushing the class whose back button text you're expecting to change. – RyJ Jun 15 '12 at 23:56
  • Hey guys, @alyoka has a non-code solution; just scroll below. The one with the image. – doekman Nov 16 '12 at 20:59
  • Perhaps worth noting: in your "source" controller (the one before the push, eg: "Controller A") you don't need to mess around with `viewWillDisappear`, `viewWillAppear`, etc. you can actually put it in the init method, eg: `initWithNibName:bundle:` – taber Mar 28 '13 at 21:36
  • @Marco The button do not retains the one side triangle shape, the common rectangular shape is applied. ¿Have anyone the tip for retain the triangle shape? – rolivares May 02 '13 at 02:27
  • It really doesn't work. Push A to B, that code should be at A right? anyway... I put it that code both A and B but it shows just Back. I changed text of backBarButtonItem : @"test" – LKM Aug 08 '15 at 17:09
  • I realize Bill above said this, but it is easy to miss. You set this on the CONTROLLER THAT PUSHES A NEW ONE, not on the controller being pushed. In my case, I generally set this in viewDidLoad in my "Parent" VC!! – EricWasTaken Mar 11 '16 at 21:42
97

Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:

(bottom)  ControllerA -> ControllerB  (top)

...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.

So it's more in the context of self.title, not like the other left/right-bar-button setters.

Marco
  • 14,977
  • 7
  • 36
  • 33
  • 13
    This is a very important detail. – Bill May 26 '11 at 22:43
  • 4
    wcochran, it does work with storyboard, just make sure that you set Back Button of the parent controller. So if you have controller A that points to controller B (via storyboard segue), than in order to customize text for Back button in controller B, you have to set "Back Button" property for controller A in storyboard editor. – interrupt Mar 05 '13 at 18:03
  • Thanks Marco. What I felt about this question was that everybody is trying to answer (and answer right) but not for the question that is being asked. You basically hit the nail in head and correctly. Thanks for that! – theiOSguy Aug 03 '13 at 22:44
  • +1 for important detail. The above answer won't work for you unless you understand THIS answer. – baptzmoffire Oct 29 '13 at 22:46
  • You're right, the keyboard on the new MacBook One is a bit hard to get used to. – Mason G. Zhwiti Aug 21 '15 at 15:23
48

You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.

enter image description here

doekman
  • 18,750
  • 20
  • 65
  • 86
alyoka
  • 821
  • 7
  • 6
  • This was exactly what I needed since I wanted all the back buttons to have the Text "Back" instead of the previous controller's nav titles. – Dave May 10 '12 at 01:59
  • 8
    Just to note, the back button text is set on the view which the next controller was pushed from, not the view controller you currently have shown. – Darbio Jun 18 '13 at 11:04
  • This is a good simple solution, but *please* don't go renaming all your back buttons to "Back", it goes against Apple's HIG and defeats part of the purpose of a back button. :( – Samuel Goodwin Aug 08 '13 at 14:29
  • Worked great. Just be sure to SAVE the Storyboard file. My first attempt did not work because I did not notice the Storyboard had not saved! And yes, you change the BACK button on the view controller you are coming from! – EricWasTaken May 10 '15 at 04:49
  • Very well, but what if I want to leave just arrow, without any text ? :) – Yuriy Vasylenko Sep 25 '15 at 21:16
  • If you want just an arrow and no text then you should enter a **space** in that field (not leaving it empty). – algrid Sep 15 '17 at 13:26
40

Thanks Marco... that helped me...

Here is what i did.

If you are using a tableView to navigate to different views... put the code:

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

In your didSelectRowAtIndexPath method... of the first Controller... Controller A.

When you navigate to Controller B the button will have the title "Back".

Dan VanWinkle
  • 991
  • 11
  • 20
Ben Call
  • 426
  • 4
  • 2
12

The back button pulls its text from the title of the parent view controller.

In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.

For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."

in the viewDidLoad method of RootViewController.m:

self.title = @"Home";

in the viewDidLoad method of SecondViewController.m:

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

If you want your back button to read, "Back," set the title of the parent view controller to @"Back";

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
10

This work better for me. Try :

 self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
Felipe FMMobile
  • 1,641
  • 20
  • 17
9

If you are using storyboard:

  1. Open StoryBoard
  2. In Document Outline window find ViewController to which you want to return to
  3. Click on Navigation Item of that ViewController
  4. In Attributes explorer change Back Button value to your custom tile

That is it, enjoy...

AntonijoDev
  • 1,317
  • 14
  • 29
5

And in MonoTouch the following works (in ViewDidLoad of the parent controller):

NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);
BillF
  • 444
  • 1
  • 9
  • 15
3
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
  initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];

This worked for me.

DisplayName
  • 3,093
  • 5
  • 35
  • 42
3

In your parent view controller, set the back button when view loads:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.navigationItem.backBarButtonItem = 
       [[UIBarButtonItem alloc] initWithTitle:@"title" 
                                        style:UIBarButtonItemStylePlain 
                                       target:nil 
                                       action:nil];

}

Notice that we don't need to include autorelease at the end with the latest iOS version.

Hope this helps!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
1

[self.navigationController.navigationBar.backItem setTitle:@"back"];

It works for me. You can replace "back" with something else.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
ohmer
  • 51
  • 4
  • When answering with code, always explain what the problem with the provided code was and how your code fixes the issue. – Lukas Knuth Mar 15 '13 at 12:37
1

This one worked for me if you don't want to have a title!

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
gpichler
  • 2,181
  • 2
  • 27
  • 52
1
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
user2783798
  • 101
  • 1
  • 2
0

I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!

self.navigationItem.title = @"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
user2547667
  • 231
  • 1
  • 3
  • 7
0

My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:

So I put the title change in calling view controller when I push the other view controller like:

self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:@"pushedVCIdentifier"];
self.pushedVC.delegate = self;   
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = @"Back";

and in delegate callback function (which I invoke in viewWillDissapear):

-(void)pushedVCWillClose:(PushedVC *)sender
{
    self.title = @"Previous Title";
}
Despotovic
  • 1,807
  • 2
  • 20
  • 24
0

If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".

jianpx
  • 3,190
  • 1
  • 30
  • 26
0

For to change back button title, refer below code

InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];

//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`
Pang
  • 9,564
  • 146
  • 81
  • 122
Kiran K
  • 919
  • 10
  • 17
0

//You can achieve this by setting the title in the previous view controller as shown below

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for this view
        self.navigationItem.title = "My Title"

}

override func viewWillDisappear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for back button in next view 
        self.navigationItem.title = "Back"
}
John Carto
  • 165
  • 1
  • 4
0

In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003

Michael Lin Liu
  • 131
  • 2
  • 2
-1

This worked for me:

self.navigationController.navigationBar.topItem.title = "Back"
Pang
  • 9,564
  • 146
  • 81
  • 122
Rocky
  • 2,903
  • 1
  • 22
  • 26