Question

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,

Was it helpful?

Solution

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];

OTHER TIPS

self.navigationController.navigationBar.topItem.title = @"";

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

Try this hope it will be work

UIBarButtonItem *btn = 
        [[UIBarButtonItem alloc] initWithTitle:@"New Title" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:btn];

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

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)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

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)

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)
}

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.

in AppDelegate in the DidFinishLaunchingWithOptions add this code:

[[UIBarButtonItem appearance] 
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000.0, 0.0) 
forBarMetrics:UIBarMetricsDefault];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top