2

Well, back bar buttons title changes to "Back" when title of the previous viewController is too long to fit the space within the button.

I want to change the title to something else when it says "Back", so I thought I can do something like that:

if navigationItem.backBarButtonItem.title == "Back" {
    navigationItem.backBarButtonItem.title = "SomeThing"
}

But title (or its parent view) always returns nil. Also, where to put the code: in the pushing or pushed viewController?

Thanks.

EDIT: The approved answer in the link

How to set back button text in Swift

is different, because I want to change the title only in the case it is too long to fit inside the button label.

Ali_Habeeb
  • 205
  • 2
  • 9
  • Look at this: https://stackoverflow.com/a/32284525/1133047 – Dinsen Oct 13 '18 at 19:54
  • Possible duplicate of [How to set back button text in Swift](https://stackoverflow.com/questions/28471164/how-to-set-back-button-text-in-swift) – Roel Koops Oct 13 '18 at 19:56
  • @Dinsen It is different. I want to change title only when it is too long. – Ali_Habeeb Oct 13 '18 at 20:49
  • Well then try find out how many characters it needs before it change the title to back and then make an if statement where you check for that amount of characters. – Dinsen Oct 14 '18 at 15:27
  • 1
    @Dinsen It returns `nil` all the time, as I mentioned in the question. It seems that a new object is created by the "pushed" viewController in this case. Besides, what you suggested is not effective, because not only the backButton label matters, but also the title of the "pushed in" viewController: It also changes to "Back" when the title of the controller is long. – Ali_Habeeb Oct 15 '18 at 05:23
  • Show us your code – Dinsen Oct 15 '18 at 06:23
  • Hi @Ali_Habeeb, did you find solution for it? – Carrione Dec 01 '22 at 18:17

0 Answers0