42

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,

rustylepord
  • 5,681
  • 6
  • 36
  • 49
S S
  • 646
  • 1
  • 7
  • 16

11 Answers11

118

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];
rustylepord
  • 5,681
  • 6
  • 36
  • 49
34
self.navigationController.navigationBar.topItem.title = @"";
Kampai
  • 22,848
  • 21
  • 95
  • 95
Alex_Burla
  • 800
  • 1
  • 9
  • 9
  • Awesome fix... Thanks – sheetal Feb 24 '16 at 19:49
  • 2
    In my case `navigationController?.navigationBar.backItem?.title = ""` for back button – Mathias Claassen Apr 11 '16 at 17:51
  • 7
    The problem with this approach would be , lets say you have a home view controller with the title "Home" , Then you navigate to the second view controller which has the title "Child" , when you apply this , it will change the back button title , however when you go back, it will change the home screen title back button title . ;) unless you don't set it explicitly again . – rustylepord Jun 03 '16 at 17:03
  • This is a much better solution – Sergiob Jan 30 '18 at 08:32
  • it changes tittle in the middle of bar not back button's title – user25 Mar 24 '19 at 12:56
16

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

P.L.
  • 1,498
  • 14
  • 18
5

Try this hope it will be work

UIBarButtonItem *btn = 
        [[UIBarButtonItem alloc] initWithTitle:@"New Title" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:btn];
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
  • 1
    add this line in view did load method from where you are switching next viewcontroller. like you are going to detailview controller(having back button) from master view controller then add it in masterview controller viewdidload method @SeemaSharma – Gajendra Rawat Apr 17 '14 at 13:10
3

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

Brian
  • 301
  • 3
  • 10
  • 1
    It doesn't work. Where do you call it? I tried `viewDidLoad` and `viewWillAppear` but nothing happens when it calls `self.navigationController?.navigationBar.backItem?.title = "test"` – user25 Mar 24 '19 at 12:58
3

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)
rjobidon
  • 3,055
  • 3
  • 30
  • 36
1

swift 2.0:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.topItem?.title = ""
}

Note: It works only if storyboard have a chain of navigation stack.

Other options/changing title:

self.navigationController?.navigationBar.backItem?.title = ""
navigationItem.backBarButtonItem?.title = ""
navigationItem.leftBarButtonItem?.title = ""

Removing navigationItem:

navigationItem.setLeftBarButtonItem(nil, animated: true)
shim
  • 9,289
  • 12
  • 69
  • 108
Alvin George
  • 14,148
  • 92
  • 64
1
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Bobj-C
  • 5,276
  • 9
  • 47
  • 83
WoShiNiBaBa
  • 257
  • 5
  • 19
1

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)
}
PMW
  • 11
  • 3
-1

To remove back button title for all view controller add new swift file and copy this extinction to it

    import UIKit

    extension UIViewController {
        static func swizzle(){


            let orginalSelector = #selector(viewDidLoad)
            let swizzledSelector = #selector(swizzledViewDidLoad)

            let orginalMethod = class_getInstanceMethod(UIViewController.self, orginalSelector)
            let swizzledMethod = class_getInstanceMethod(UIViewController.self, #selector(swizzledViewDidLoad))

            let didAddMethod = class_addMethod(UIViewController.self, orginalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))

            if didAddMethod {
                class_replaceMethod(UIViewController.self, swizzledSelector, method_getImplementation(orginalMethod!), method_getTypeEncoding(orginalMethod!))
            }else{
                method_exchangeImplementations(orginalMethod!, swizzledMethod!)
            }

        }

        @objc func swizzledViewDidLoad(){
            navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
            swizzledViewDidLoad()
        }


    }

After that in AppDelegate inside didFinishLaunchingWithOptions, call the swizzle function.

UIViewController.swizzle()

This function is using the objective c runtime to exchange the viewDidLoad method with another one that removes the back button title and then inside it recall the original viewDidLoad.

Abedalkareem Omreyh
  • 2,180
  • 1
  • 16
  • 20
-3

in AppDelegate in the DidFinishLaunchingWithOptions add this code:

[[UIBarButtonItem appearance] 
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000.0, 0.0) 
forBarMetrics:UIBarMetricsDefault];