Gruntfluffle
Gruntfluffle

Reputation: 231

How can I remove the text from the navigation bar back item

I'm trying to remove the text "Back" from the navigation back button, leaving just the back chevron, but everything I'm trying is not working. For example if I add something like the following, obtained from previous answers to the same question, to viewDidLoad:

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

or

navigationController?.navigationBar.backBarButtonItem = UIBarButtonItem(title: "go away", style: .plain, target: nil, action: nil)

Then when the view appears it's still showing "< Back" in the navigation bar.

Here's what the views look like within captured within viewDidAppear. Image:1

Upvotes: 1

Views: 85

Answers (4)

Predrag Samardzic
Predrag Samardzic

Reputation: 3009

Alternatively, from Interface Builder, you can set previous UIViewController's Back Button on Navigation Item to " " (not empty string, space):

enter image description here

Upvotes: 0

Luis Ramirez
Luis Ramirez

Reputation: 1604

You are changing the wrong thing. You use this code here to change the title for the back button.

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .done, target: self, action: #selector(handleBack))

By doing this you need to add a selector for the button as well. Cause if you click the back button nothing will happen. This is how you would do that.

@objc private func handleBack() {
    navigationController?.popViewController(animated: true)
}

Hope this helps.

Upvotes: 0

Jawad Ali
Jawad Ali

Reputation: 14397

Try this code snippet hope it will help you

happy coding =)

override func viewDidLoad() {

        DispatchQueue.main.async {

        if let navBar = self.navigationController?.navigationBar {
            navBar.backItem?.title = ""
           }
        }
    }

Upvotes: 1

claude31
claude31

Reputation: 884

You should create a left button and set the action to return to the rootViewController. In viewDidLoad:

let leftButton = UIBarButtonItem(title: "<", style: .plain, target: self, action: #selector(back(_ :)))
self.navigationItem.leftBarButtonItem  = leftButton

Upvotes: 0

Related Questions