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: UIBarButtonIt
For the case where we have not control at all of the previous view controller (i.e. if we are working in a framework), we can delete the title of the back button as follow:
// For iOS 10
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = String()
// For iOS 11
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = nil
What it does is to navigate to the last item of the navigation's stack and delete its back title. Make sure to save the original one when our view controller will appear:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
originalBackButtonTitle = navigationController?.navigationBar.items?.last?.backBarButtonItem?.title
// Delete title somewhere here...
}
and then reassign it, in order to not disrupt any part of the app:
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = originalBackButtonTitle
super.viewWillDisappear(animated)
}