'backBarButtonItem in iOS (Swift)
var backButton: UIBarButtonItem = UIBarButtonItem(title: "CUSTOM", style: UIBarButtonItemStyle.Bordered, target: self, action: nil)
newNavigationItem.backBarButtonItem = backButton
Where is newNavigationItem: UINavigationItem
I want custom back title. It shows "Back" text or "Title of last view". So it does not work. Why?
Solution 1:[1]
Use leftBarButtomItem
newNavigationItem.backBarButtonItem = backButton
should be
newNavigationItem.leftBarButtonItem = backButton
Solution 2:[2]
override func viewDidLoad() {
super.viewDidLoad()
let newBackButton = UIBarButtonItem(title: "Back",
style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
navigationController?.navigationBar.topItem?.backBarButtonItem = newBackButton
}
func backAction() -> Void {
self.navigationController?.popViewControllerAnimated(true)
}
Solution 3:[3]
Setting custom UIBarButtonItem as backBarButtonItem in my case had some padding from left. After close inspection it showed default image from Back Button was visible.
What worked for me:
navigationController?.navigationBar.backIndicatorImage = UIImage()
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage()
navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "backIcon"), style: .plain, target: self, action: nil)
With this logic swipe back is working normally and also click on back button pops viewcontroller.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yogesh Suthar |
| Solution 2 | Mannam Brahmam |
| Solution 3 | Arijan |
