How do I change the title of the “back” button on

2019-01-01 14:13发布

Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).

I want to change the text shown on the button to something else.

I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.

self.navigationItem.leftBarButtonItem.title = @"Log Out";

What should I do?

Thanks.

30条回答
宁负流年不负卿
2楼-- · 2019-01-01 15:01

I had a parent view controller with a really long title. This resulted in the back button text bleeding into the title of the child view controller.

After trying a bunch of different solutions, this is what I ended up doing (expanding on the @john.k.doe approach):

Using Xcode 7.2, Swift 2

  1. In the Storyboard, add a Navigation Item to the Parent View Controller scene (not the child VC)

navigation item

  1. On the Attributes Inspector of your new Navigation Item, type in a space character in the Back Button field. More on this later.

navigation item in View Hierarchy

add a space character to the Back Button field

  1. In the Parent view controller, add the following code:

snippet:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segue.destinationViewController {
    case is ChildViewController:
        navigationItem.backBarButtonItem?.title = ""
    default:
        navigationItem.backBarButtonItem?.title = "Full Parent Title"
    }
}

Explanation:

The back button sort of belongs to the parent view controller. The Navigation Item gives you a handle to the back button, so you can set the title in code or in the Storyboard.

Note:

If you leave the Navigation Item Back Button text as the default empty string, the back button title will become "Back".

Other approaches work, why use this one?:

While it's possible to override the back button title on the child view controller, it was a challenge getting a handle to it until it had already flashed briefly on the screen.

Some of the approaches construct a new back button and override the existing one. I'm sure it works, and probably necessary in some use cases. But I prefer to leverage existing APIs when possible.

Changing the title of the parent view controller is the quickest solution for some situations. However, this changes the parent title so you have to manage state. Things also get messy with a Tab Bar Controller because title changes cause side effects with the Tab Bar Item titles.

查看更多
不再属于我。
3楼-- · 2019-01-01 15:01
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];
查看更多
旧人旧事旧时光
4楼-- · 2019-01-01 15:03

Here is the answer:

In viewDidAppear:animated (NOT in viewDidLoad) do the following

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

That if you want to keep the shape of the back button.

查看更多
柔情千种
5楼-- · 2019-01-01 15:04

Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title.
So to keep the original style there are 2 ways:
1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow.
2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed.
Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).

- (void)viewDidLoad {
    [super viewDidLoad];
    static NSString * backButtonTitle=@"Back"; //or whatever u want

    if (![self.title isEqualToString:backButtonTitle]){

        UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
        customTitleView.text = self.title; // original title
        customTitleView.font = [UIFont boldSystemFontOfSize:20];
        customTitleView.backgroundColor = [UIColor clearColor];
        customTitleView.textColor = [UIColor whiteColor];
        customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        customTitleView.shadowOffset = CGSizeMake(0, -1);

        [customTitleView sizeToFit];

        self.navigationItem.titleView = [customTitleView autorelease];
        self.title = backButtonTitle; 
    }
}

This solution works good and it looks native. Also if use it in the viewDidLoad method it prevents execution more then 1 time.
Also I've tried a Jessedc's solution but it looks bad. It causes visible to user title bar change on the fly from original to BackButton's desired and back.

查看更多
皆成旧梦
6楼-- · 2019-01-01 15:04

Use below line of code :

UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"hello"
                                 style:UIBarButtonItemStylePlain
                                target:nil
                                action:nil];

self.navigationItem.leftBarButtonItems =[[NSArray alloc] initWithObjects:newBackButton, nil];

self.navigationItem.leftItemsSupplementBackButton = YES;
查看更多
心情的温度
7楼-- · 2019-01-01 15:05

self.navigationController.navigationBar.backItem.title = @"TEXT";

And in Swift:

self.navigationController?.navigationBar.backItem?.title = "TEXT"
查看更多
登录 后发表回答