【发布时间】:2014-06-09 18:11:50
【问题描述】:
我遇到了一个问题,当我将项目推到导航控制器上时,后退按钮只会显示“后退”。我尝试设置断点并检查堆栈上的导航项。堆栈上的所有项目都有一个 nil backButtonItem 和一个标题。我什至尝试设置 backBarButtonItem 但仍然只是说“返回”。其他人有这个问题吗?
【问题讨论】:
我遇到了一个问题,当我将项目推到导航控制器上时,后退按钮只会显示“后退”。我尝试设置断点并检查堆栈上的导航项。堆栈上的所有项目都有一个 nil backButtonItem 和一个标题。我什至尝试设置 backBarButtonItem 但仍然只是说“返回”。其他人有这个问题吗?
【问题讨论】:
iOS 7 会自动将您的后退按钮标题替换为“返回”,甚至完全删除标题以适应当前导航项的标题。您可能不应该尝试对此做任何事情,除非尝试缩短您的标题。
【讨论】:
您需要将每个UIViewController 的title 属性设置为您希望后退按钮显示的内容。
相关:View Controller Catalog article 记录了此行为。
【讨论】:
在 iOS 7 中,前一个控制器导航项的标题属性会更改下一个控制器中的后退按钮。基本上,后退按钮的标题就是上一页的标题。
但是,如果您希望后退按钮的标题与前一个控制器的标题不同,最好的选择是将该控制器的导航项标题视图设置为UILabel。然后,您可以将该控制器的导航项标题属性设置为后退按钮应显示的任何内容。创建具有正确字体和大小的标签的示例代码:
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!";
【讨论】: