2

There are few similar kind of questions asked and answered by many on Stackoverflow. But none of solution matches with my requirement.

I am using Swift3 for IOS mobile app development and used Navigation controller to manage the navigation. I gave title to all pages using below code.

self.title = "Title"

When I move to next page, then it shows me back button with the earlier page title. For some pages, title is long and it disturbs my header section of page.

Instead of page title, I want to change button text to "Back". Any idea how to do that?

4
  • you want title name "back" instead of previous vc name ? Commented May 1, 2017 at 6:27
  • No. I want button text as "Back" and not the page title as back. Commented May 1, 2017 at 6:29
  • hide back button and make new left bar button with title back or custom back button Commented May 1, 2017 at 6:30
  • Possible Duplicate of Swift renaming the back navigation item Commented May 1, 2017 at 6:30

5 Answers 5

3

if you are using storyboard then you can set back button title there, click on ViewController -> Navigation Item -> Back Button and set "Back" title.

enter image description here

alternatively you can set title in ViewWillApear and change it to "Back" in viewWillDisappear method

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

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.title = "Back"
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I was expecting. It worked perfectly with adding those 2 method. Though I didn't find "Navigation Item" in Xcode.
If you have embedded your ViewController in UINavigationController then you will find it below your View hierarchy or when you tap on navigation bar area in your ViewController.
1

In your prepare function before the segue do this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Back" // Change to desired title here
    navigationItem.backBarButtonItem = backItem
}

Comments

1

Try to remove the title on viewWillDisapperar then re enter it on viewWillAppear

Comments

1

Try this:

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

It should work :)

1 Comment

It set earlier page title as Back when click on Back button
0

Try this :

self.navigationController!.navigationBar.topItem!.title = "Back"

1 Comment

It set earlier page title as Back when click on Back button

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.