6
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?

Max
  • 1,341
  • 3
  • 20
  • 39

4 Answers4

15

The method that Yogesh Suthar said will replace the whole back button, so you will not have the default left arrow sign! I suggest to use this code:

navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
Majid
  • 3,128
  • 1
  • 26
  • 31
  • Do you put this in the parent view controller? – hungrxyz Oct 09 '15 at 20:08
  • Yeah. You can do this anywhere that you have access to the navigationViewcontroller. – Majid Oct 10 '15 at 02:44
  • This should be the accepted answer. Setting the leftBarButtonItem is a workaround. In swift 2 I used this in the viewDidLoad of the view controller pushing to the one I wanted to change the back title on: `navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "Back", style: .Plain, target: nil, action: nil)` – earnshavian Nov 04 '15 at 18:29
  • @Majid +1 for best answer. Now I can also use this solution. :) – Yogesh Suthar Dec 31 '16 at 12:20
  • this answer is obsolete – Gargo Jul 25 '23 at 11:15
5

Use leftBarButtomItem

newNavigationItem.backBarButtonItem = backButton

should be

newNavigationItem.leftBarButtonItem = backButton
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Thanks. Thats work. For what backBarButtonIteam right now? Also one question: what need to put in "Action" that this button work like usually Back button – Max Jul 12 '14 at 13:57
  • Ah. There also no arrow left more. How to add it? – Max Jul 12 '14 at 14:00
  • 1
    @MaximDroy Why aren't you setting the title property instead? No need to create a whole new UIBarButtonItem. See: http://stackoverflow.com/questions/20282760/back-text-displayed-in-ios7-navigationbar-when-view-title-is-long – klcjr89 Jul 12 '14 at 14:24
  • 2
    @MaximDroy Sorry for late reply. For your first question about "Action", you need to create a custom function and you need to pop VC, you can use this code `self.navigationController.popViewControllerAnimated(true)`. – Yogesh Suthar Jul 12 '14 at 14:47
  • Setting leftBarButtonItem is a workaround - instead set the backBarButtonItem to a custom UIBarButtonItem in the viewDidLoad of the view controller pushing to the one you want to change as suggested by @Majid below http://stackoverflow.com/a/30250603/169902 – earnshavian Nov 04 '15 at 18:32
  • Using this solution will disable swipe back action. – Arijan Mar 04 '22 at 20:09
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)
}
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
1

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.

Arijan
  • 297
  • 3
  • 6