简体   繁体   中英

iOS7 UINavigationItem back button title not working

I've been having an issue where when I push items on to the navigation controller and the back button just says "Back". I'm tried setting a break point and checking the navigation items on the stack. All items on the stack have a nil backButtonItem and a title. I even tried settings the backBarButtonItem but am still getting it just saying "Back". Anyone else had this problem?

iOS 7 will automatically replace your back button title with "Back" or even remove the title altogether in order to fit the title of current navigation item. You probably shouldn't try to do anything about it except maybe try and make your titles shorter.

You need to set each UIViewController 's title property to what you want the back button to say.

Related: View Controller Catalog article that documents this behaviour.

In iOS 7. the previous controller navigation item title property changes the back button in the next controller. Basically, the back button title is the title of previous page.

However, if you want different title for the back button than the previous controller's title, the best option is to set that controller's navigation item title view with an UILabel . Then you may set that controller's navigation item title property to anything the back button should display. Example code creating the label with the proper font and size:

NSString * title = @"Title of page";
NSDictionary * titleAttribs = navigationController.navigationBar.titleTextAttributes;
UILabel * titleLabel = [[UILabel alloc] init];
NSAttributedString * titleAttrString = [[NSAttributedString alloc] initWithString:title attributes:titleAttribs];

// the attributed text misses the bold attribute (because bold is not considered as font attribute in Cocoa)
titleLabel.attributedText = titleAttrString;

// get font and make it bold
UIFont * font = titleLabel.font;
UIFontDescriptor * fontDesc = [font.fontDescriptor
                               fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFont * boldFont = [UIFont fontWithDescriptor:fontDesc size:0]; // size:0 means keep the size as is
titleLabel.font = boldFont;
[titleLabel sizeToFit];


anotherController.navigationItem.titleView = titleLabel; // this will be the title in NavBar
anotherController.navigationItem.title = @"Go back"; // this will be the title of the back button

[navigationController pushViewController:anotherController animated:YES];
self.navigationController.navigationBar.backItem.title = @"Back!";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM