251

How do you remove the back button text.

Current back button:

< Back

Desired back button:

< AnythingElse

None of these have worked:

self.navigationItem.backBarButtonItem?.title = "Back"
self.backItem?.title = ""
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationItem.backBarButtonItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title="Back"
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title
HangarRash
  • 7,314
  • 5
  • 5
  • 32
Onichan
  • 4,476
  • 6
  • 31
  • 60

28 Answers28

678

The back button belongs to the previous view controller, not the one currently presented on screen.
To modify the back button you should update it before pushing, on the view controller that initiated the segue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Something Else"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

Swift 3, 4 & 5:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Something Else"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

OR

// in your viewDidLoad or viewWillAppear
navigationItem.backBarButtonItem = UIBarButtonItem(
    title: "Something Else", style: .plain, target: nil, action: nil)

PhillipJacobs
  • 2,337
  • 1
  • 16
  • 32
Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44
  • 13
    Thanks a lot, you don't know how many hours I spend just for that small detail (the previous controller) – Ricardo Feb 07 '16 at 16:29
  • 3
    If I could upvote a million times I would. Can't tell you how long I spent wondering why it wasn't working (was previously on destination controller) – Dylan Ireland May 24 '16 at 21:18
  • 38
    "The back button belongs to the previous view controller" that's the most valuable answer that i've found about this topic! Thank you! – vhristoskov Dec 01 '16 at 14:38
  • I think it's ok to set it this way. But I recommend you, if possible set the `title` of your previous `viewController` in the navigation stack. That's much better approach. – Mohammad Abdurraafay Apr 10 '17 at 16:37
  • 1
    Setting the `title` changes the *title*, not just the back button. – Yariv Nissim Apr 11 '17 at 00:39
  • 5
    I added self.navigationController!.navigationBar.topItem!.backBarButtonItem = backItem instead of navigationItem.backBarButtonItem = backItem and it works fine on Swift 3. Thank you – Indra Adam May 06 '17 at 10:35
  • This is the right to do it. Also I'd like to point out if you are not using segue to pop out views, it still works as long as you change the backBarButtonItem before popping out your view. – Oliver Zhang Jun 29 '17 at 03:18
  • 1
    Interestingly, if you want to title to be nothing you have to set it to " ", not "". "" alone leaves a lot of space, intruding on the title view. – E-Madd Aug 12 '17 at 00:05
  • this is the only solution that worked for me. thanks a lot – Matthew Usdin Nov 11 '17 at 22:43
  • What should I do if I don't use segues? – Ivan Smetanin Apr 06 '18 at 13:26
  • 1
    Instead of `prepareForSegue`, set the back button before pushing the new view controller. – Yariv Nissim Apr 06 '18 at 17:48
  • 1
    This can be done in a single line of code: navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil) – softwarenerd Jun 21 '19 at 07:00
  • Is there a way to do this in SwiftUI? – devlo Jan 28 '21 at 18:14
136

You can do it from interface builder as follows:

click on the navigation item of previous view controller

enter image description here

from the attributes inspector set the back button text to whatever you want. Thats it!!

enter image description here

Ajinkya Patil
  • 5,518
  • 4
  • 20
  • 22
66

You can put this 3 line of code in the ViewController you want to change the back button title.

In your override func viewDidLoad() {}.

let backButton = UIBarButtonItem()
backButton.title = "My Back Button Title"
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
Hitesh Chauhan
  • 1,520
  • 15
  • 16
  • 3
    This solution seems more correct, as you put the logic in the arriving VC not the VC you are leaving. Aka: reads better. – Sentry.co Apr 26 '18 at 11:36
  • 3
    Working solution, but with a big caveat: remember that the back button still belongs to the *previous* view controller. For example if your navigation is A > B < back to A > C, and you put this code in B, also the back button in C will say "My Back Button Title" – Alessandro Mulloni Jan 17 '21 at 15:04
55

Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.

self.navigationItem.title = ""

Another way is to put

self.navigationController?.navigationBar.topItem?.title = ""

in current view controller's viewWillAppear method. This one will cause some other problem if navigation stack is too nested.

minhazur
  • 4,928
  • 3
  • 25
  • 27
  • 5
    self.navigationItem.title = "" should be on top as the best answer! – 0xRLA May 07 '16 at 16:45
  • I do agree with this as the best answer cause all answers above require adding a target function to the navigation item , so they are not just change the text but they add a custom pop function as well , which is not required by the question ! – IsPha Jan 17 '17 at 08:03
  • This works fine in `prepareForSegue`, which is. a little cleaner because you're not altering the new viewController after it loads. – Adrian May 08 '18 at 02:20
  • Pay Attention!!! put self.title = "....." before the calling to self.navigationItem.title = "" – Bary Levy Oct 18 '18 at 12:59
44

If you are using xib file for view controller then do this in your view controller class.

class AboutUsViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    edgesForExtendedLayout = []
    setUpNavBar()
}

func setUpNavBar(){
    //For title in navigation bar
    self.navigationController?.view.backgroundColor = UIColor.white
    self.navigationController?.view.tintColor = UIColor.orange
    self.navigationItem.title = "About Us"

    //For back button in navigation bar
    let backButton = UIBarButtonItem()
    backButton.title = "Back"
    self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
}

}

The result will be:

enter image description here

mustaq
  • 961
  • 14
  • 16
31

I do not know where you have used your methods that you put on your question but I could get the desired result if I use, on my ViewController class (in which I want to change the back button), on viewDidLoad() function, the following line:

self.navigationController?.navigationBar.backItem?.title = "Anything Else"

The result will be:

Before

enter image description here

After

enter image description here

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • 3
    In my case it only worked if you place this piece of code on the viewDidAppear method and not the viewDidLoad one – apinho Oct 11 '17 at 13:32
  • 2
    Use `navigationController.navigationBar.topItem?.title = ""` instead to avoid putting this in `viewDidAppear` – José May 10 '18 at 10:36
  • This seems the most satisfactory way and avoids a flash as the back button title is changed to " " using self.navigationItem.title = " " in the calling controller (slower devices). Also the other situation where self.navigationItem.title = " " is completely ignored intermittently transitioning quickly into the view controller via a tab controller tab switch programatically. – FlimFlam Vir Jan 13 '20 at 17:00
24

The back button belongs to the previous view controller, not the one currently presented on screen. To modify the back button you should update it before pushing, add viewdidload :

Swift 4:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
Mohammad Razipour
  • 3,643
  • 3
  • 29
  • 49
12

You can just modify the NavigationItem in the storyboard

enter image description here

In the Back Button add a space and press Enter.

Note: Do this in the previous VC.

xhinoda
  • 1,032
  • 1
  • 19
  • 26
9

This should work:

override func viewDidLoad() {
    super.viewDidLoad()

    var button = UIBarButtonItem(title: "YourTitle", style: UIBarButtonItemStyle.Bordered, target: self, action: "goBack")
    self.navigationItem.backBarButtonItem = button

}

func goBack()
{
    self.navigationController?.popViewControllerAnimated(true)
}

Although it is not recommended since this actually replaces the backButton and it also removed the back arrow and the swipe gesture.

Dejan Skledar
  • 11,280
  • 7
  • 44
  • 70
9

Swift 4.2

If you want to change the navigation bar back button item text, put this in viewDidLoad of the controller BEFORE the one where the back button shows, NOT on the view controller where the back button is visible.

 let backButton = UIBarButtonItem()
 backButton.title = "New Back Button Text"
 self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

If you want to change the current navigation bar title text use the code below (note that this becomes the default back text for the NEXT view pushed onto the navigation controller, but this default back text can be overridden by the code above)

 self.title = "Navigation Bar Title"
Trev14
  • 3,626
  • 2
  • 31
  • 40
ikbal
  • 1,110
  • 12
  • 21
8

Swift 4 - Configure the back button before pushing any view controllers

// if you want to remove the text
navigationItem.backBarButtonItem = UIBarButtonItem()

// if you want to modify the text to "back"
navigationItem.backBarButtonItem = UIBarButtonItem(title: "back", style: .plain, target: nil, action: nil)
budiDino
  • 13,044
  • 8
  • 95
  • 91
8

There are two ways.

1.In the previousViewController.viewDidLoad()

let backBarBtnItem = UIBarButtonItem()
backBarBtnItem.title = "back"
navigationItem.backBarButtonItem = backBarBtnItem

2.In the currentViewController.viewDidAppear()

let backBarBtnItem = UIBarButtonItem()
backBarBtnItem.title = "back"      
navigationController?.navigationBar.backItem?.backBarButtonItem = backBarBtnItem

Reason : the backButton comes from navigationBar.backItem.backBarButtonItem,so the first way is obvious.In currentViewController.viewDidLoad(),we can't obtain the reference of backItem,because in viewDidAppear(),the navigationBar pushed navigationView on its stack.so we can make changes to the backItem in currentViewController.viewDidAppear()

For more details,you can see Document:UINavigationBar

Calcifer
  • 81
  • 1
  • 2
6

Back button text and color text:

navigationController?.navigationBar.tintColor = .red
navigationController?.navigationBar.topItem?.backButtonTitle = "Hi"
oskarko
  • 3,382
  • 1
  • 26
  • 26
3

In the viewDidLoad method of the presenting controller add:

// hide navigation bar title in the next controller
let backButton = UIBarButtonItem(title: "", style:.Plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backButton
Claus
  • 5,662
  • 10
  • 77
  • 118
3

although these answers fix the problem but this could be some useful

class MainNavigatioController: UINavigationController {

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // first
        let backItem = UIBarButtonItem()
        backItem.title = "رجوع"
        self.viewControllers.last?.navigationItem.backBarButtonItem = backItem
        // then
        super.pushViewController(viewController, animated: animated)

    }

}
Ahmad Labeeb
  • 1,056
  • 11
  • 21
3

Swift 4

While the previous saying to prepare for segue is correct and its true the back button belongs to the previous VC, its just adding a bunch more unnecessary code.

The best thing to do is set the title of the current VC in viewDidLoad and it'll automatically set the back button title correctly on the next VC. This line worked for me

navigationController?.navigationBar.topItem?.title = "Title"
Joel
  • 327
  • 4
  • 3
3

It works for me. Swift 5

navigationItem.backButtonTitle = ""
Oleh H
  • 846
  • 1
  • 10
  • 18
2

This works for Swift 5:

self.navigationItem.backBarButtonItem?.title = ""

Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!

Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).

Saeed Ir
  • 1,974
  • 2
  • 20
  • 22
  • 1
    That didn't change anything. this works: navigationItem.backButtonTitle = "" – Tilo Delau Nov 03 '20 at 15:35
  • It will be effective for the next pushed view controller not the current one on the display – Saeed Ir Nov 04 '20 at 18:40
  • No, I put it everywhere, still doesn't work. It returns nil, so i don't even have one. I think this is for if you have customised and added your own button. I am manipulating the "real" one. if you change text on backButtonTitle, does it do anything for you? I think they are two different. – Tilo Delau Nov 05 '20 at 14:17
1

Try this... it will work ....

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    self.title = ""
}

The above code will hide the text and show only the back arrow on navigation bar.

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
1

Swift 4

In my case the solution was to clear the navigation item of the Master View Controller before move to the Child View Controller. And set it again if it is shown again

MasterController

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationItem.title = "Master Title"
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationItem.title = ""
}

And this is how I push a UIViewController and clear the back bar button item in the child controller:

MasterController

let childController = ChildController(collectionViewLayout: UICollectionViewFlowLayout())
childController.navigationItem.backBarButtonItem?.title = ""
navigationController?.pushViewController(childController, animated: true)
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
1

Following code can be added to a view controller from where you are pushing view controller in which you want to change back button text

Swift 5

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)

For example:-

  1. ViewController1
  2. ViewController2

Assume we want to update back title of viewcontroller2 and we are pushing viewcontroller2 from viewcontroller1.

then you can use following code:-

let vc2 = storyboard.instantiateViewController(withIdentifier: "ViewController2")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:  "your custom back button title", style: .plain, target: self, action: nil)
self.navigationController?.pushViewController(vc2, animated: true)
Harshit
  • 107
  • 1
  • 6
1

There has two of different ways to Implanting  that part those are ,  

1.

navigationItem.backButtonTitle = "Title Goes Here"

(swift 5)

2.

let backButton = UIBarButtonItem()
backButton.title = "Title Goes Here"
navigationController?.navigationBar.topItem?.backBarButtonItem = backButton
Zaporozhchenko Oleksandr
  • 4,660
  • 3
  • 26
  • 48
0

Set self.title = "" before self.navigationController?.pushViewController(vc, animated: true).

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
米米米
  • 970
  • 7
  • 9
0
override func viewWillAppear(_ animated: Bool) {

    self.tabBarController?.navigationItem.title = "Notes"

    let sendButton = UIBarButtonItem(title: "New", style: .plain, target: self, action: #selector(goToNoteEditorViewController))

    self.tabBarController?.navigationItem.rightBarButtonItem = sendButton
}

func goToNoteEditorViewController(){
   // action what you want
}

Hope it helps!! #swift 3

0

If you are pushing a view controller from page view controller page, you cannot update the navigation controller's back button title. To solve this create a delegate back to your parent view controller (you may also be able to traverse the view controller hierarchy back up to the parent).

Furthermore, Back buttons have a character limit. If you exceed that character limit, the system will default to "Back". It will not truncate for you. For example:

backItem.title = "Birthdays/Anniversaries" // Get's converted to "Back".
backItem.title = "Birthdays/Anniversa…" // Fits and shows as is.
Justin Domnitz
  • 3,217
  • 27
  • 34
0

for Swift 4.2

let backItem = UIBarButtonItem()
backItem.title = ""
navigationItem.backBarButtonItem = backItem
Iryna Batvina
  • 1,656
  • 13
  • 7
0

Solution checked and work in Swift 5

Below I put few solutions for different cases:

1. Remove text from back button

The best solution to remove text from back button is to add in viewDidLoad():

navigationItem.backBarButtonItem = UIBarButtonItem()

2. Set own text on back button

In case you want to set your own title, do it by setting title of backButton:

let backButton = UIBarButtonItem()
backButton.title = "My Title"
navigationItem.backBarButtonItem = backItem

3. Empty back button on all VC

If you want to create common style in entire app - to have just arrow back without text, create base VC for all your View Controllers:

class BaseViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.backBarButtonItem = UIBarButtonItem()
    }
}

Solution presented above let you customize back button in the future if you want to make some exception later, by adding additional variable and overriding it in specific ViewController, f.ex:

class BaseViewController: UIViewController {

    var customBackButtonTitle: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        var backButton = UIBarButtonItem()
        if let text = customBackButtonTitle {
            backButton.title = text
        }
        navigationItem.backBarButtonItem = backButton
    }
}
lukszar
  • 1,252
  • 10
  • 13
0

GOTCHA: If you are having trouble with any of the many-starred suggestions, ensure that you are registering your UITableViewCells in viewDidLoad(), not from init()

Womble
  • 4,607
  • 2
  • 31
  • 45