Reputation: 83
So I want to change the text on the back button of a UIViewController, and I want it to satisfy the following criteria:
So far the answer I found is either How do I change the title of the "back" button on a Navigation Bar which is unable to change the text dynamically
or this https://stackoverflow.com/a/498089/2925345 which removes the swipe gesture.
Is there any way to satisfy both of the criterias?
Thanks
Upvotes: 0
Views: 2092
Reputation: 83
Finally solved this by assigning a delegate to UINavigation Controller's interactivePopGestureRecognizer
.
And it must be executed in or after viewWillAppear
, in this particular order
self.navigationItem.backBarButtonItem = backButton;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
Once I did that I can change the title of backBarButtonItem
however I want.
Got my answer from
I don't know how it works, I don't know why simply assigning a delegate to a swipe gesture recognizer enables the title of back button to be customizable, I simply followed the instructions in this magical blog post. Thanks zhaoxy_thu for making this work.
Upvotes: 0
Reputation: 471
Try this
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle" style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
Upvotes: 0
Reputation: 3873
If not overridden, left button in navigation bar is constructed from navigationItem.backBarButtonItem
of previous view controller. To change the text, you need to do something like
NSInteger count = self.navigationController.viewControllers.count;
if (count >= 2) {
UIViewController* previousVC = self.navigationController.viewControllers[count-2];
previousVC.navigationItem.backBarButtonItem.text = @"new text";
}
Upvotes: 1