【问题标题】:How to set large title on navigation bar for iOS 11?如何在 iOS 11 的导航栏上设置大标题?
【发布时间】:2017-09-22 13:09:14
【问题描述】:

我正在为 iOS 11 或更高版本创建应用程序,需要在导航栏左侧设置大标题。

请高人帮忙弄清楚如何设置,它应该只适用于 iOS 11 或更高版本。

请给我一些其他建议,以在整个应用程序(支持 iOS 8 或更高版本)中保持此功能可用。

提前致谢。

【问题讨论】:

标签: ios uinavigationcontroller ios11


【解决方案1】:

这里是代码 sn-p,用于在 iOS 11 或更高版本的导航栏左侧显示大标题。

目标 C:

  self.title = @"Your title";
    if (@available(iOS 11, *)) {
        self.navigationController.navigationBar.prefersLargeTitles = true;
         self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
    }

斯威夫特:

       self.title = "Your title"
        if #available(iOS 11, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationController?.navigationItem.largeTitleDisplayMode = .always
        } 

您需要在构建应用程序之前为 iOS 11 设置检查条件。

测试大标题的要求:

  • Xcode 9.0,
  • Mac OSX - 10.12.6 或更高版本,
  • 装有 iOS 11 的 iPhone/iPad 或 Xcode 9 模拟器。

【讨论】:

  • 我的标题很大,我怎样才能让它适合屏幕?
【解决方案2】:
if (@available(iOS 11.0, *)) {
    [[UINavigationBar appearance] setPrefersLargeTitles:false];
}

【讨论】:

  • 不应该设置为true吗?
【解决方案3】:

您可以将 UILabel 设置为 titleView

UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = YOUR_TITLE_TEXT;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.font = FONT_NAV_BAR;
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;

【讨论】: