Remove text from Back button keeping the icon

I want to remove the text from the back button, but I want to keep the icon. I have tried

let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton

However, this removes completely the text and the icon.


Solution 1:

I know this already has an answer, but you can also do it in code (in case you're working with nibs)

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

Add the above in the first view controller.

Note that you need to do this for each view controller that is pushing. So if you have 3 view controllers, and you want to remove the back text from all of them, you'll have to add the line in view controller 1 and 2.

Solution 2:

The method of @rmd2 is almost right, but instead you should select the navigation bar of the controller to which back button will point to and type " " in the Back Button field.

enter image description here

Solution 3:

After Searching a lot I found best and simple solution, this will affect all the viewControllers written in Swift 4.2 and also working in Swift 5

extension UIViewController {
    open override func awakeFromNib() {
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }  
}

Solution 4:

The text of the back button depends on the title of the master view.

The trick is to clear the title if the master view disappears and set it again if it is shown again:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // needed to clear the text in the back navigation:
    self.navigationItem.title = " "
}

override func viewWillAppear(_ animated: Bool) {
   
    super.viewWillAppear(animated)
    self.navigationItem.title = "My Title"
}