let's say a class named 设置 is going to push SubSettingsVC then in the subSettingsVC back button will show a title <Settings so in order to remove the "settings" text from back button and make it something like <
you've to set 项目 title in SettingsVC's
ViewWillDisaper () 方法。
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"
}
extension UIViewController {
open override func awakeAfter(using coder: NSCoder) -> Any? {
navigationItem.backButtonDisplayMode = .minimal // This will help us to remove text
return super.awakeAfter(using: coder)
}
}
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).
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
}
}
}