In my application I want to use 'Back' text as back button title for every viewcontroller. I have read so many posts on stackoverflow but got nothing.
I don't want to set leftbarbuttonitem.
Can anyone help me on this simple task.
Thanks,
If you want to change the navigation bar back button item text, put this in viewDidLoad of the controller BEFORE the one where the back button shows, NOT on the view controller where the back button is visible.
Storyboard. You can also set this in the Storyboard. Select UINavigationBar and select the Attributes Inspector tab. Then you can change those two images under Back and Back Mask attributes.
Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.
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];
self.navigationController.navigationBar.topItem.title = @"";
If you are using storyboard you can select the navigation item in the parent view controller and set the button text you want in 'Back Button' field. Remember to set this in the parent view controller, not in the child that is pushed.
Try this hope it will be work
UIBarButtonItem *btn =
[[UIBarButtonItem alloc] initWithTitle:@"New Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:btn];
I needed to use self.navigationController.navigationBar.backItem.title = @"";
, the difference being that I'm using backItem
instead of topItem
.
self.navigationController.navigationBar.topItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain
target:nil action:nil];
self.navigationController?.navigationItem.backBarButtonItem =
UIBarButtonItem(title:"Title", style:.plain, target:nil, action:nil)
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Title",
style:.plain, target:nil, action:nil)
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Title",
style:.done, target:nil, action:nil)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
Changes the currently visible Back Button
extension UIViewController {
func setCurrentBackButton(title: String) {
guard let vcCount = self.navigationController?.viewControllers.count else {
return
}
let priorVCPosition = vcCount - 2
guard priorVCPosition >= 0 else {
return
}
self.navigationController?.viewControllers[priorVCPosition].navigationItem.backBarButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With