1

I want to hide the text of the back button on the navigation bar and so have found past questions such as this: UINavigationBar Hide back Button Text

However I can't change the text at all, either via using the storyboard, or in code. See screenshot below for attempt at changing it using the storyboard:

enter image description here

Or if I try to do it programatically by adding the following to viewDidLoad of the pushed view controller

 self.navigationItem.backBarButtonItem?.title = "stuff"

It has no effect, nor does moving the same line of code to the view controller doing the pushing.

How come it won't change at all regardless of how I'm trying to change it? How come using the storyboard, the navigation item title can be set, but not the back button text?

If I add the following to the pushed view controller then I can get the text to change:

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted)

But I would like to understand why none of the other ways of trying to change it have any effect

Community
  • 1
  • 1
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

3 Answers3

1

The title of the back button gets automatically set to the title of the view controller that it will go back to.

To do what you want, you'll have to hide the back button and insert your own button with your own image.

Annoying == @YES.

Brett Donald
  • 6,745
  • 4
  • 23
  • 51
0

As Brett mentioned above, a new bar button must be created to change the text.

To set the title of the back button, try the following code:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Stuff" style:UIBarButtonStylePlain target:nil action:nil];

Or make it in storyboard by adding a bar button item to your navigation bar.

importnumpyasnp
  • 262
  • 5
  • 14
0

When it comes to segue from tabBarController to a normal navigation controller, it is always easy to get confused in implementing backBarItem.

The trick is about which controller the backBarItem belongs to. If we navigate from controller A to controller B, then the backBarItem, which is the back button appearing on the controller B's navigation bar, actually belongs to controller A. So we just need to find the right controller to edit the backBarItem.

Solution 1. In the controller A, set the backBarButton self.tabBarController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)

   //M: In controller A
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tabBarController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
    }

Solution 2. We can use a customised leftBarButton in controller B to cover controller A's backButton. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "<", style: .done, target: self, action: #selector(tapBackButton)), then set the action of the leftBarButton to go back to the previous controller.

    //M: in Controller B   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //M: Hide the default back button.
        //M: backBarItem will be covered by the leftBarItem anyway, here is to add an extra handling.
        navigationItem.hidesBackButton = true
        
        //M: Customize a leftBarButton.
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "   < ", style: .done, target: self, action: #selector(tapBackButton))
        
        //M: Customize the color and font size to the leftBarButton
        navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 25)], for: .normal)
    }
    
    //M: Set the action of the leftBarButton to go back.
    @objc func tapBackButton(_ sender:Any){
        self.navigationController?.popToRootViewController(animated: true)
    }
Michael Lin Liu
  • 131
  • 2
  • 2