1

For some reason this code won't hide back button text.

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)}

Text remains "Back".

On the other hand executing:

self.navigationItem.hidesBackButton

does hide the button. I execute both inside viewDidLoad. Any ideas how to remove that text and keep the arrow only? I read the suggestions on this site already.

Teddy
  • 1,056
  • 1
  • 14
  • 24

4 Answers4

0

Swift 4:

/// This will hide the back button title
private func hideBackButtonTitle(){
    let backButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
    backButtonItem.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
    backButtonItem.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .highlighted)
    self.navigationItem.backBarButtonItem = backButtonItem
}
Lloyd Keijzer
  • 1,229
  • 1
  • 12
  • 22
0

Add this simple line of code in your root/main ViewController -

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)

Code -

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
    }

OUTPUT -

enter image description here

Rashed
  • 2,349
  • 11
  • 26
0

I am using the following technique to hide the back button. Whenever i perform segue OR push a new view controller to the navigation stack, i call this function and pass self to the parameter.

func clearBackButtonText(vc : UIViewController) {

  let backItem = UIBarButtonItem()
  backItem.title = ""
  vc.navigationItem.backBarButtonItem = backItem

}
//call this function in view didLoad of your view controller OR before pushing a view controller on navigation stack
clearBackButtonText(vc: self)

See the Example

On Viewdidload of TableVC, i am calling the function

class TableVC: UIViewController {
   override func viewDidLoad() {

       super.viewDidLoad()
       clearBackButtonText(vc: self)
    }
}

And On didSelect of a row, i am doing a push segue to DetailVC. And my back button is only arrow

enter image description here

Hope it helps

Awais Fayyaz
  • 2,275
  • 1
  • 22
  • 45
0

Thanks for the responses. It turned out that the title of the back button will be set to the title of the view controller that it will go back to. All I had to do is to set the navigationItem.title = " " for the previous ViewController.

Super frustrating...

Teddy
  • 1,056
  • 1
  • 14
  • 24