How to Set Back Button Text in Swift

How to set back button text in Swift

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)

How to change back button text in Navigation item to picture in xcode

Add below code in viewDidLoad of viewController where you want to change back button title:

 let backButton = UIBarButtonItem()
backButton.title = ""
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

Sample Image

Swift 4 remove text from back button

There are a couple ways to achieve what you want to do.

When you push a view controller (VC2) onto the navigation stack for the first controller (VC1) the back button uses the title of the first screen by default as the back button title when the second screen is presented.

Sample Image

So in VC1 you could set the view controller's title to an empty string before you push onto the stack

override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
}

and this will leave you with a back button that just has the '<' character.

Sample Image

The second method is to set a custom back bar button item in the parent view controller (VC1)

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
}

Sample Image

which sets the back button title to whatever you set in your UIBarButtonItem title field.

So your implementation might look something like this: (pick one option or the other where commented, if you do both the custom bar button item overrides the title.)

@IBAction func didSelectSegment(_ sender: UISegmentedControl) {

let vcName = vcNames[sender.selectedSegmentIndex]
if vcName == "NQTPastRecordViewController" && loggined() == false {
let alert = alertAskLogin({
let signInVC = getController("EVSignInViewController")

// set title to empty string here
title = ""

// or set a custom back bar button item and set target/action as needed
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

navigationController?.pushViewController(signInVC, animated: true)
})
self.present(alert, animated: true, completion: nil)
segmentControll.selectedSegmentIndex = 0
return
}
container.swipeToController(at: sender.selectedSegmentIndex)

}

I want to set back button text on navigation bar But I can't. This Code Not Working

Edit

As per the comment of @Boobesh, the solution that worked as per the question:

let vc = ViewController() 
self.navigationItem.backBarButtonItem = UIBarButtonItem(title : "Back", style:.plain, target:nil, action:nil)
self.navigationController?.pushViewController(vc, animated: true)

Original Answer

Try adding this code in the view controller one level up the stack.

If you are moving from controller A to B, write this code on A to see the effect on B.

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

Swift Custom NavBar Back Button Image and Text

You can do something like that:

let yourBackImage = UIImage(named: "back_button_image")
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationController?.navigationBar.backItem?.title = "Custom"

Your image will only have one color though

UINavigationController back button custom text?

From this link:

self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];

As Tyler said in the comments:

don't do this in the visible view controller, but in the view
controller that you'd see if you hit the back button



Related Topics



Leave a reply



Submit