Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UINavigationBar back button title

In my application I want to use 'Back' text as back button title for every viewcontroller. I have read so many posts on stackoverflow but got nothing.

I don't want to set leftbarbuttonitem.

Can anyone help me on this simple task.

Thanks,

like image 562
S S Avatar asked Apr 17 '14 12:04

S S


People also ask

How do I change the back button text in Swift?

If you want to change the navigation bar back button item text, put this in viewDidLoad of the controller BEFORE the one where the back button shows, NOT on the view controller where the back button is visible.

How do I add a back button to my storyboard?

Storyboard. You can also set this in the Storyboard. Select UINavigationBar and select the Attributes Inspector tab. Then you can change those two images under Back and Back Mask attributes.

How do I create a custom navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


8 Answers

Do this in the parent view controller not in the child

Swift

navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

Objetive-C

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
like image 188
rustylepord Avatar answered Oct 11 '22 17:10

rustylepord


self.navigationController.navigationBar.topItem.title = @"";
like image 23
Alex_Burla Avatar answered Oct 11 '22 17:10

Alex_Burla


If you are using storyboard you can select the navigation item in the parent view controller and set the button text you want in 'Back Button' field. Remember to set this in the parent view controller, not in the child that is pushed.

enter image description here

like image 34
P.L. Avatar answered Oct 11 '22 18:10

P.L.


Try this hope it will be work

UIBarButtonItem *btn = 
        [[UIBarButtonItem alloc] initWithTitle:@"New Title" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:btn];
like image 36
Gajendra Rawat Avatar answered Oct 11 '22 19:10

Gajendra Rawat


I needed to use self.navigationController.navigationBar.backItem.title = @"";, the difference being that I'm using backItem instead of topItem.

like image 33
Brian Avatar answered Oct 11 '22 19:10

Brian


Back Button With Back Arrow

Objective-C

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

Swift

self.navigationController?.navigationItem.backBarButtonItem = 
UIBarButtonItem(title:"Title", style:.plain, target:nil, action:nil)

Normal Button Without Back Arrow

Objective-C

self.navigationItem.leftBarButtonItem = 
[[UIBarButtonItem alloc] initWithTitle:@"Title" 
style:UIBarButtonItemStylePlain target:nil action:nil];

Swift

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Title", 
style:.plain, target:nil, action:nil)

Bold Button Without Back Arrow

Objective-C

self.navigationItem.leftBarButtonItem = 
[[UIBarButtonItem alloc] initWithTitle:@"Title" 
style:UIBarButtonItemStyleDone target:nil action:nil];

Swift

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Title", 
style:.done, target:nil, action:nil)
like image 23
rjobidon Avatar answered Oct 11 '22 17:10

rjobidon


navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
like image 43
WoShiNiBaBa Avatar answered Oct 11 '22 18:10

WoShiNiBaBa


Changes the currently visible Back Button

extension UIViewController {
func setCurrentBackButton(title: String) {
    guard let vcCount = self.navigationController?.viewControllers.count else {
        return
    }

    let priorVCPosition = vcCount - 2

    guard priorVCPosition >= 0 else {
        return
    }

    self.navigationController?.viewControllers[priorVCPosition].navigationItem.backBarButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
}
like image 36
PMW Avatar answered Oct 11 '22 17:10

PMW