Rectangle 27 16

You need to set up a custom stack of UINavigationItem objects and push them on to the UINavigationBar. This is the only way I know of to get a true back button. I haven't tested this code, but you should do something like this:

UINavigationItem *previousItem =
    [[[UINavigationItem alloc] initWithTitle:@"Back title"] autorelease];

UINavigationItem *currentItem =
    [[[UINavigationItem alloc] initWithTitle:@"Main Title"] autorelease];

[navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil]
               animated:YES];

To handle when the buttons are pressed you should set yourself as the navigation bar's delegate and implement the UINavigationBarDelegate delegates.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.'

what is the currentItem?

objective c - Draw custom Back button on iPhone Navigation Bar - Stack...

iphone objective-c uinavigationcontroller uinavigationbar back-button
Rectangle 27 9

For those using storyboards just select the parent (not the one that is holding target view) view controller frame (be sure you click right on the Navigation bar, then open attributes inspector, where you'll find three form inputs. The third one "back button" is that we are looking for.

this is the best solution if you don't care about localization.

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 747

navigationItem.hidesBackButton = true

@user8170 how to hide the left barbuttonitem on navigation bar..?

@vakio that does not actually work ;)

self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;

In iOS 7, self.navigationItem.leftBarButtonItem = nil; does not affect the back button when issued from the viewDidLoad, viewWillAppear, or viewDidAppear. This line of code does work within the viewdidLoad: self.navigationItem.hidesBackButton = YES;

not sure if this helps but 'self' is always a view controller which is being presented on the top of stack of navigation controller.

objective c - How to hide 'Back' button on navigation bar on iPhone? -...

iphone objective-c navigation
Rectangle 27 3

I've found that it is best to change the title of the current view controller in the navigation stack to the desired text of the back button before pushing to the next view controller.

self.navigationItem.title = @"Desired back button text";
[self.navigationController pushViewController:QAVC animated:NO];

Then in the viewDidAppear set the title back to the desired title for the original VC. Voila!

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 327

This should be placed in the method that calls the ViewController titled "NewTitle". Right before the push or popViewController statement.

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];

When in the child view controller, calling this from the child view controller will not work. You have to call this from the parent view controller, while within the child view controller.

To change the back button for my third level view controller (second push) I had to use the following line instead of the above answer: [[self.parentViewController navigationItem] setBackBarButtonItem: newBackButton]; Am I doing something wrong?

Jordan says he "[added] a little more clarity on where to put the code." Here is even more clarity: put it in the first (aka parent) controller, immediately before the call to push the second (aka child) controller.

you can create a custom BaseViewController and put it in to -viewDidLoad:

You don't have to do this right before push or pop. You can do it in viewDidLoad or loadView or wherever else you set the title and other properties for this view. Victor Bogdan's answer below is clearer, IMHO.

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 1

Hi, i think your answer can help me in my problem, please how to use the backBarButtonItem in my code, assuming that this is my code in the previous view: -(IBAction)goToRechercherView { rechercherViewController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:rechercherViewController animated:YES]; }

objective c - Draw custom Back button on iPhone Navigation Bar - Stack...

iphone objective-c uinavigationcontroller uinavigationbar back-button
Rectangle 27 78

self.navigationController.navigationBar.topItem.title = @"Back";

Works in Swift too!

self.navigationController!.navigationBar.topItem!.title = "Back"

Tested and verified on iOS 7 in Xcode 5.0.2. This WILL change the previous screen's title to Back when you go back to it though.

Simpliest solution. Works on both iOS 6 and 7. Put in the base UIViewController and done. Much better than the IB solution or setting in the pushing vc.

it changes title of previous view controller, that's the problem in this solution

@Nikita, You can set the tile back in viewWillDisappear

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 70

Here is the documentation for backBarButtonItem:

"When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead."

self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

When any other view controller B is on top of the navigation stack, and A is right below it, B's back button will have the title "Short".

Good answer! But what's the purpose of this behavior?

Giving a custom title to the button.

the tricky part is you need to set the back button in the PARENT view controller and not the child view. Wasted an hour just figured this out.

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 57

in Xcode 4.5 using storyboard, by far the easiest solution i've found when the value of the Back button doesn't have to change dynamically is to use the "Back Button" field associated with the Navigation Item of the View Controller to which you want the "Back" button to say something else.

e.g. in the screenshot below, i want the Back button for the view controller(s) that i push to have "Back" as the title of the Back button.

of course, this won't work if you need the back button to say something slightly different each time there are all of the other solutions here for that.

Same in Xcode 6 Beta 7.

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 35

The best way is to combine these, so it will hide the back button even if you set it up manually :

self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;

To answer user716216, he explained it in his one sentence explanation. The second line of code is what works for the default case. The first line of code works if you have created and added the button yourself.

objective c - How to hide 'Back' button on navigation bar on iPhone? -...

iphone objective-c navigation
Rectangle 27 39

I know, the question is very old, but I found a nice solution.

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

Works from childView! Tested with iOS 7.

This only seems to work to one level. It is not working for a child of a child for me. iOS 8.

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 25

Maybe I'm being over simplistic but From Apple's documentation the wording is:

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controllerthat is, the view controller one level down on the stack.

The solution marked correct above sets a default button item from the parent controller. It's the right answer, but I'm solving the issue by changing self.title property of the UIViewController right before pushing the new controller onto the NavigationController stack.

This automatically updates the back button's title on the next controller, and as long as you set self.title back to what it should be in viewWillAppear I can't see this method causing too many problems.

Not a very beautiful way

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 17

[self.navigationItem setHidesBackButton:YES animated:YES];
[self.navigationItem setHidesBackButton:YES];

Also if you have custom UINavigationBar then try bellow code

self.navigationItem.leftBarButtonItem = nil;

This is the way to do it 'dynamically', or 'on demand'.

in any class when you used that code then on that class's navigationbar back button will hide ...

objective c - How to hide 'Back' button on navigation bar on iPhone? -...

iphone objective-c navigation
Rectangle 27 361

Try putting this into the view controller where you want to detect the press:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
       // back button was pressed.  We know this is true because self is no longer
       // in the navigation stack.  
    }
    [super viewWillDisappear:animated];
}

+1 great hack, but does not offer control over animation of pop

Doesn't work for me if I send a message to the delegate through a button and the delegate pops the controller - this still fires.

Another problem is that you can't differentiate if the user pressed the back button or if you programatically called [self.navigationController popViewControllerAnimated:YES]

if (find(self.navigationController!.viewControllers as! [UIViewController],self)==nil)

iphone - Setting action for back button in navigation controller - Sta...

iphone cocoa-touch uinavigationcontroller uibarbuttonitem
Rectangle 27 18

self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 17

Ok, here is the way. If you have a view controller "first" and you navigate another view controller "second" by pushing a button or etc. you need to do some work. First you need to create a BarButtonItem in "second" view controller's ViewDidLoad method like this;

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];

After you do that, you need to write to code for "btnBack" action in the same .m file like this;

-(IBAction)OnClick_btnBack:(id)sender  {
      [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

A leftBarButtonItem does not have the appearance of a backBarButtonItem, if that is important in the application design.

Of the solutions posted (thank you very much) this one seems to work, albeit as Alex stated above, the left bar button doesn't look like a back bar item. How can I make it look like a back bar item???

This really worked for me ..Thanks a lot ..

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 16

In Swift/iOS8, the following worked for me:

let backButton = UIBarButtonItem(
      title: "Back Button Text",
      style: UIBarButtonItemStyle.Bordered,
      target: nil,
      action: nil
);

self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;
self.navigationController?.navigationBar.topItem?.backBarButtonItem

Thanks for this. What a truly pathetic API this is. And representative of UIKit in general. Between navigation bars and Status Bars in iOS, it's a wonder we still develop for this goddamned platform. Thank baby jesus for Stack Overflow and its contributors!

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 13

I had a parent view controller with a really long title. This resulted in the back button text bleeding into the title of the child view controller.

After trying a bunch of different solutions, this is what I ended up doing (expanding on the @john.k.doe approach):

  • In the Storyboard, add a Navigation Item to the Parent View Controller scene (not the child VC)
Attributes Inspector
Navigation Item
space
Back Button
  • In the Parent view controller, add the following code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segue.destinationViewController {
    case is ChildViewController:
        navigationItem.backBarButtonItem?.title = ""
    default:
        navigationItem.backBarButtonItem?.title = "Full Parent Title"
    }
}

The back button sort of belongs to the parent view controller. The Navigation Item gives you a handle to the back button, so you can set the title in code or in the Storyboard.

If you leave the Navigation Item Back Button text as the default empty string, the back button title will become "Back".

While it's possible to override the back button title on the child view controller, it was a challenge getting a handle to it until it had already flashed briefly on the screen.

Some of the approaches construct a new back button and override the existing one. I'm sure it works, and probably necessary in some use cases. But I prefer to leverage existing APIs when possible.

Changing the title of the parent view controller is the quickest solution for some situations. However, this changes the parent title so you have to manage state. Things also get messy with a Tab Bar Controller because title changes cause side effects with the Tab Bar Item titles.

iphone - How do I change the title of the "back" button on a Navigatio...

iphone objective-c uinavigationbar uinavigationitem
Rectangle 27 159

navigationShouldPopOnBackButton
UIViewController
-(BOOL) navigationShouldPopOnBackButton {
    if(needsShowConfirmation) {
        // Show confirmation alert
        // ...
        return NO; // Ignore 'Back' button this time
    }
    return YES; // Process 'Back' button click and Pop view controler
}

This is the cleanest solution I've seen, better and simpler than using your own custom UIButton. Thanks!

navigationBar:shouldPopItem:
UINavigationBarDelegate

I just implemented this (pretty cool BTW) in iOS 7.1 and noticed that after returning NO the back button stays in a disabled state (visually, because it still receives and reacts to touch events). I worked around it by adding an else statement to the shouldPop check and cycling through the navigation bar subviews, and setting the alpha value back to 1 if needed inside an animation block: gist.github.com/idevsoftware/9754057

iphone - Setting action for back button in navigation controller - Sta...

iphone cocoa-touch uinavigationcontroller uibarbuttonitem
Rectangle 27 1

For a form that requires user input like this, I would recommend invoking it as a "modal" instead of part of your navigation stack. That way they have to take care of business on the form, then you can validate it and dismiss it using a custom button. You can even design a nav bar that looks the same as the rest of your app but gives you more control.

iphone - Setting action for back button in navigation controller - Sta...

iphone cocoa-touch uinavigationcontroller uibarbuttonitem