How to Change back button title on navigation controller in swift3?
Asked Answered
C

8

7

I want to change the title of backbutton to any name in swift 3.I tried many ways but none of them worked for me.

self.navigationItem.backBarButtonItem?.title="Title"
self.navigationController?.navigationItem.backBarButtonItem?.title="Title"

Just for information i have written below code in appdelegate.

let backImage : UIImage = UIImage(named:"backArrow")!
UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, 0), for: .default)
IQKeyboardManager.sharedManager().enable = true
self.window?.backgroundColor = UIColor.white
Conjunct answered 9/11, 2016 at 10:33 Comment(4)
check this https://mcmap.net/q/109716/-how-to-set-back-button-text-in-ios-navigation-controller.Beanstalk
Possible duplicate of How to set back button text in SwiftBlubber
please reffer might answer you questionLeathern
check my updated answer....Hostetler
E
10

Navigation item back button name will be same as the title of previous view controller which is pushing it to the navigation stack :)

So if VC A pushes VC B, back button in VC B will be A.

So all you can do is, to change the title of the previous viewController before pushing the new viewController using code :)

self.navigationItem.title = "ABCD"

And in ViewWillAppear of VC A,you can revert the title back to whatever it was earlier :)

self.navigationItem.title = "Back to xyz"

All that being said, if you don't want all this circus :) you can simply hide the default back button using,

self.navigationItem.hidesBackButton = true

in your VC B, create a UIBarButton item, set whatever the title you want to set and then set that as leftBarButtonItem :) using,

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("ABCD", comment: "ABCD"), style: .plain, target: self, action:#selector(self.abcdTapped:)

of course now that will not show "<" icon :) Now if you want that as well you can add it as a image to back bar button item :) but its cumbersome :)

Hope it helps :)

Evangelineevangelism answered 9/11, 2016 at 10:38 Comment(0)
F
5

You will have to set the backBarButtonItem property of the navigationItem of the viewController that you push the said viewController from.

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

However, you must set this for each viewController.

Formative answered 10/11, 2016 at 3:51 Comment(0)
J
4

This is the way:

extension UINavigationController {
    func addCustomBackButton(title: String = "Back") {
        let backButton = UIBarButtonItem()
        backButton.title = title
        navigationBar.topItem?.backBarButtonItem = backButton
    }
}
Juror answered 20/3, 2019 at 9:23 Comment(0)
C
3

In Swift 3.0 put below code in appdelegate didFinishLaunchingWithOptions method its worked perfectly for me

let backImage = UIImage(named: "BackNavigation")?.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default)

The last line will remove the title of Navigation Back Button if you don't want to remove title then just comment it

Congelation answered 27/6, 2017 at 11:15 Comment(0)
P
2

I didn't find the answer that I was looking for so I share with you my solution.

Sometimes you have to change the text of the back button in the parent ViewController and not in the ViewController where seems to be defined the back button, remember that a navigation controller stacks ViewControllers one after another.

In my case I did this on the function prepare(for segue: ) of the "ParentViewController":

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showChildViewController" {
        if let childViewController = segue.destination as? ChildViewController {
            let backItem = UIBarButtonItem()
            backItem.title = "Back"
            navigationItem.backBarButtonItem = backItem
        }
    }
Proxy answered 15/5, 2018 at 19:46 Comment(0)
H
0

Try following steps to set image to your back button..

Output:

enter image description here

Step 1:

Add following code to your AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    var backButtonImage = UIImage(named: "Your Image Name")
    backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 0)
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
    UINavigationBar.appearance().tintColor = UIColor.white

    return true
}

Step 2:

Add following code to your MainVC

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    title = "Title 1"
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 20)!]
  }

Step 3:

Add following code to your DestVC or 2ndVC

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    title = "Title 2"
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 20)!]
  }

Step 4:

Select your navigationBar from your StoryBoard and goto Attribute Inspector. Under Navigation Item change your Back Button name enter a empty space or programatically create a back button with plain title..

enter image description here

Step 5:

Add icon image to your Assets. 1x29pt,2x58pt and 3x87pt. I am not sure about the asset image size.Check with apple doc about the size class..

enter image description here


Update:

My Similar answer related to this post.

How to customize the navigation back symbol and navigation back text?

Hostetler answered 9/11, 2016 at 11:15 Comment(0)
V
0

You can easily do that from the storyboard by setting the title of the previous screen. Image explaining how to do that from storyboard - or you can do that by adding the following code to the view controller you're navigating BACK to.

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)
        let backItem = UIBarButtonItem()
        backItem.title = "Title"
        navigationItem.backBarButtonItem = backItem
    }
Variation answered 18/5, 2020 at 14:30 Comment(0)
F
0

in viewDidLoad()

    let backBarButtonItem = UIBarButtonItem(title: "You back button title here", style: .plain, target: nil, action: nil)
    navigationItem.backBarButtonItem = backBarButtonItem
Fitton answered 2/7, 2020 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.