Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).
I want to change the text shown on the button to something else.
I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.
self.navigationItem.leftBarButtonItem.title = @"Log Out";
What should I do?
Thanks.
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):
Using Xcode 7.2, Swift 2
Navigation Item
to the Parent View Controller scene (not the child VC)Attributes Inspector
of your newNavigation Item
, type in aspace
character in theBack Button
field. More on this later.snippet:
Explanation:
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.Note:
If you leave the
Navigation Item
Back Button
text as the default empty string, the back button title will become "Back".Other approaches work, why use this one?:
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 aTab Bar Controller
because title changes cause side effects with theTab Bar Item
titles.Here is the answer:
In
viewDidAppear:animated
(NOT inviewDidLoad
) do the followingThat if you want to keep the shape of the back button.
Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title.
So to keep the original style there are 2 ways:
1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow.
2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed.
Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).
This solution works good and it looks native. Also if use it in the viewDidLoad method it prevents execution more then 1 time.
Also I've tried a Jessedc's solution but it looks bad. It causes visible to user title bar change on the fly from original to BackButton's desired and back.
Use below line of code :
self.navigationController.navigationBar.backItem.title = @"TEXT";
And in Swift: