How to Change Text on a Back Button

How to change text on a back button

Try

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

I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationItem.

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

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];

Change the font on the back button of a navigation controller under swift 4

For Swift 4, U can try this in AppDelegate.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 15)!], for: UIControlState.normal)

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font : UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)! ], for: .focused)

return true
}

In ViewController.

override func viewWillAppear(_ animated: Bool) {

self.title = "List"
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!]

// THE BELOW THREE LINES NOT WORKING.

//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .normal)

//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .highlighted)

//navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 20)!], for: .focused)

}

Storyboard

Sample Image

Output

Sample Image

How to change the Back Button Text on Xamarin.Forms

You have to set the Backbutton title of your previous page as string.Empty. By default it's the title of the previous page on the navigation stack that is shown as the back button text.

Also Android does not support this property.

Default text for back button in NavigationView in SwiftUI

I've managed to localize back buttons by providing translations for the Back key in the Localizable.strings file.

I am using SwiftUI though.

Can't change text of navigation item back button

The title of the back button gets automatically set to the title of the view controller that it will go back to.

To do what you want, you'll have to hide the back button and insert your own button with your own image.

Annoying == @YES.

Swift change text of back button

As the title of the back bar button of the destination controller displays automatically the title of the navigation item of the source controller, a way is to set the title of the navigation item in the source view controller

  • in viewWillAppear() or viewDidAppear() to the normal title.
  • in prepareForSegue() to the title the back bar button is supposed to display.

How can I change the font of the back button for my navigation bar?

Just tested your code and it seems the reason that line is returning nil is actually because name: "FONTNAME" returns nil. So if you set that name attribute to a valid font name, the code should run without an error -- even if navigationController?.navigationItem.leftBarButtonItem is explicitly set to nil.

But regardless, also as I've seen through testing, this line won't give you the result you apparently want. The leading navigationController optional shouldn't be there since it accesses your UINavigationController and not the current view. Simply use the UIViewController's own navigationItem property to access its leftBarButtonItem directly, ex:

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)

Edit: From the comment you posted under this answer, it seems as if you don't actually want to set the leftBarButtonItem but the backBarButtonItem because you don't want to implement a custom action beyond going back to the previous view controller.

So in that previous view controller (i.e. the view before you want to show your custom back button item), you can set your custom back button without an action like so:

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)
navigationItem.backBarButtonItem = backButton

How can I change the font and color of the Navigation Bar back button

It's a UIBarButtonItem so set this property.

init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}

Or you can use this also,

init() {
let standard = UINavigationBarAppearance()

let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}


Related Topics



Leave a reply



Submit