如何更改后退按钮上的文本

默认情况下,后退按钮使用视图控制器的标题作为文本。 我可以在不更改视图控制器标题的情况下更改后退按钮上的文本吗? 我需要这个,因为我有一个视图控制器的标题太长,以显示,在这种情况下,我想显示只是“回来”作为标题的回来按钮。

我尝试了下面的方法,但是没有效果:

self.navigationItem.leftBarButtonItem.title = @"Back";

谢谢。

65440 次浏览

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.

Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:

(bottom)  ControllerA -> ControllerB  (top)

...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.

So it's more in the context of self.title, not like the other left/right-bar-button setters.

Thanks Marco... that helped me...

Here is what i did.

If you are using a tableView to navigate to different views... put the code:

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

In your didSelectRowAtIndexPath method... of the first Controller... Controller A.

When you navigate to Controller B the button will have the title "Back".

The back button pulls its text from the title of the parent view controller.

In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.

For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."

in the viewDidLoad method of RootViewController.m:

self.title = @"Home";

in the viewDidLoad method of SecondViewController.m:

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

If you want your back button to read, "Back," set the title of the parent view controller to @"Back";

And in MonoTouch the following works (in ViewDidLoad of the parent controller):

NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);

You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.

enter image description here

[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];

This worked for me.

[self.navigationController.navigationBar.backItem setTitle:@"back"];

It works for me. You can replace "back" with something else.

This work better for me. Try :

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

This one worked for me if you don't want to have a title!

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

I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!

self.navigationItem.title = @"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];

My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:

So I put the title change in calling view controller when I push the other view controller like:

self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:@"pushedVCIdentifier"];
self.pushedVC.delegate = self;
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = @"Back";

and in delegate callback function (which I invoke in viewWillDissapear):

-(void)pushedVCWillClose:(PushedVC *)sender
{
self.title = @"Previous Title";
}

In your parent view controller, set the back button when view loads:

- (void)viewDidLoad
{
[super viewDidLoad];


// Do any additional setup after loading the view.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];


}

Notice that we don't need to include autorelease at the end with the latest iOS version.

Hope this helps!

If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".

If you are using storyboard:

  1. Open StoryBoard
  2. In Document Outline window find ViewController to which you want to return to
  3. Click on Navigation Item of that ViewController
  4. In Attributes explorer change Back Button value to your custom tile

That is it, enjoy...

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

For to change back button title, refer below code

InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];


//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`

//You can achieve this by setting the title in the previous view controller as shown below

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for this view
self.navigationItem.title = "My Title"


}


override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for back button in next view
self.navigationItem.title = "Back"
}

This worked for me:

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

In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003