【问题标题】:iOS7 UINavigationItem back button title not workingiOS7 UINavigationItem后退按钮标题不起作用
【发布时间】:2014-06-09 18:11:50
【问题描述】:

我遇到了一个问题,当我将项目推到导航控制器上时,后退按钮只会显示“后退”。我尝试设置断点并检查堆栈上的导航项。堆栈上的所有项目都有一个 nil backButtonItem 和一个标题。我什至尝试设置 backBarButtonItem 但仍然只是说“返回”。其他人有这个问题吗?

【问题讨论】:

    标签: ios ios7


    【解决方案1】:

    iOS 7 会自动将您的后退按钮标题替换为“返回”,甚至完全删除标题以适应当前导航项的标题。您可能不应该尝试对此做任何事情,除非尝试缩短您的标题。

    【讨论】:

    • 谢谢,这就是问题所在。虽然它的工作原理很奇怪。它将用“Back”替换名为“The Trial”的标题,但不会替换“General Rules”....
    • 如果您认为它的行为不正常,report a bug 总是一个好主意。
    • @user2807952:你最终报告了这个错误吗?我想跟踪这个问题。
    • 您是否有指向记录此行为的任何地方的链接?
    • @SixtenOtto 阅读了UINavigationController Class Reference 中“更新导航栏”下的注释。
    【解决方案2】:

    您需要将每个UIViewController 的title 属性设置为您希望后退按钮显示的内容。

    相关:View Controller Catalog article 记录了此行为。

    【讨论】:

      【解决方案3】:

      在 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];
      

      【讨论】:

        【解决方案4】:
        self.navigationController.navigationBar.backItem.title = @"Back!";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多