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?
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?
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.
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.
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)