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
The easy programmatic way, without unwanted side-effects, is initializing the navigationItem.backBarButtonItem with an empty item in the awakeFromNib method of the source controller (the one you are navigating from):
override func awakeFromNib() {
super.awakeFromNib()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
Note: If you initialized the back button later, like in the viewDidLoad() method, than you would lose swipe-back functionality (swiping from the left edge to the right takes you one step back in the navigation stack).
Then, if you want different back button texts for different destination controllers and if you're using segues, you can set the title in the prepare(for segue:, sender:) method, like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let item = navigationItem.backBarButtonItem {
switch segue.identifier {
case "SceneOne": item.title = "Back"; break
case "SceneTwo": item.title = "Home"; break
case "SceneThree": item.title = nil; break // Use this scene's title
default: item.title = "" // No text
}
}
}