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
  • 314,917
  • 42
  • 532
  • 579
toast
  • 1,860
  • 2
  • 26
  • 49
  • Please check this my answer : - http://stackoverflow.com/a/39368022/3515115 – Kamlesh Shingarakhiya Sep 23 '16 at 04:52
  • Try this self.navigationController?.navigationBar.topItem?.title = " " – Sanjeet Verma Sep 23 '16 at 05:00
  • When trying to set navigation bar button items in the storyboard do I need to embed the secondary controller into a navigation view controller? I cant seem to add bar button items to the view controller where the navigation bar is inferred. – toast Sep 23 '16 at 06:11
  • Working solution: refer this answer https://stackoverflow.com/a/48801613/3976183 – Kumar Feb 15 '18 at 07:07

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,687
  • 1
  • 24
  • 44
  • 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
2

I think this will work for you.

self.navigationItem.hidesBackButton = true
Prashant Sharma
  • 1,357
  • 1
  • 21
  • 31
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
  • 387
  • 4
  • 18