Search code examples
iosswiftuinavigationcontrolleruinavigationbaruinavigationitem

Remove the back button text


Currently trying to remove the title of the previous VC on the back button. Following apple's documentation I have used:

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

However the back button title does not disappear! The console says it's empty (""), but it's clearly still displayed to the user.

I do not want to use hacks such as setting the previous title of previous VC to "" however currently nothing else seems to be working. Any advice?


Solution

  • Try this function as extension of UINavigationController :

    func setBackButtonTitle(_ title: String) {
        if self.navigationBar.topItem != nil {
            let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.backButtonAction))
            self.navigationBar.topItem?.backBarButtonItem = leftBarButtonItem
        } else {
            if self.navigationBar.backItem != nil {
                self.navigationBar.backItem?.title = title
            }
        }
    }