Uinavigationcontroller "Back Button" Custom Text

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

How to change the UINavigationController back button name?

In viewWillAppear write this

self.navigationItem.title = @"List View";

And in ViewWilldisapper write this

self.navigationItem.title = @"Back";

It works without story boards.

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)

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

UINavigationBar Hide back Button Text

In the interface builder, you can select the navigation item of the previous controller and change the Back Button string to what you'd like the back button to appear as. If you want it blank, for example, just put a space.

You can also change it with this line of code:

[self.navigationItem.backBarButtonItem setTitle:@"Title here"];

Or in Swift:

self.navigationItem.backBarButtonItem?.title = ""

Custom back button in UINavigationController

The backBarButtonItem property works as intended, but it will always add its standard button shape and color based on the navigation bar tint color.

You can customize the text, but not replace the image.

One workaround, as Andrew Pouliot suggested, is to use leftBarButtonItem instead, but I stuck to the standard button instead.

Storyboard UINavigation Controller Back Button Text

The back button will show the title of the previous view and if no title is set it'll show 'Back'.

So in your prepareForSegue method, you can change the title of the current view before the next view is pushed. The back button will then show whatever you set the title to.

Change UINavigationBar back button title

Do this in the parent view controller not in the child

Swift

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

Objetive-C

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

Make text as UINavigationController back button

In viewDidLoad set your custom button navBar:

let button = UIButton(type: .custom)
//Set the image
button.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
//Set the title
button.setTitle("Yourtitle", for: .normal)
//Add target
button.addTarget(self, action: #selector(callMethod), for: .touchUpInside) //call button tap action
// Add insets padding
let spacing: CGFloat = -10 // the amount of spacing to appear between image and title
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // customize your button titleEdgeInsets if you want
//Create bar button
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton

After that create your function for button tap:

@objc fileprivate func callMethod() {
print("Your code here")
}


Related Topics



Leave a reply



Submit