2

I want to know if its possible to remove the navigation bar back button text from an inherited navigation bar. Presently my navigation bar shows "< ControllerName". I want to simply show the "<" back icon. I would also like to know how to show "< Back", and how to remove it completely.

I understand I could do this by adding a bar button item to the storyboard, however is there an easier way to do it?

Note, this code does not work:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
rmaddy
  • 307,833
  • 40
  • 508
  • 550
toast
  • 1,770
  • 1
  • 21
  • 47

3 Answers3

6

You better custom back button for this task.

but You also can do it in other ways. Ex: You have ViewController1, and ViewController2 (You push ViewController2 from ViewController1)

ViewController1

public class ViewController1: UIViewController {

    override public func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.title = "viewcontroller1 title"
    }

}

ViewController2

class ViewController2: UIViewController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // get previous view controller and set title to "" or any things You want
        if let viewControllers = self.navigationController?.viewControllers {
            let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
            previousVC?.title = "" // or previousVC?.title = "Back"
        }
    }

}
larva
  • 4,582
  • 1
  • 20
  • 42
  • Thanks. this worked really well. I tried a few other examples, but none were as smooth in the transition between view controllers as this. – toast Sep 23 '16 at 07:29
  • It replaces the back navigation button with text "Back", but it also changes the page title with "Back" text. Only Navigation button text should change and not the page title. ;( – NGR May 01 '17 at 06:04
  • @NGR did U write code to change previous controller title? like in my answer ViewController1 code. – larva May 02 '17 at 15:16
1

I think this will work for you.

self.navigationItem.hidesBackButton = true
Prashant Sharma
  • 1,287
  • 1
  • 21
  • 30
0

Solution suggested by @Prashant will remove the back button from navigation bar. To remove the title, use following:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Zohaib Ejaz
  • 359
  • 4
  • 18