Use the increased navigation-bar title in iOS 11

∥☆過路亽.° 提交于 2019-11-28 16:19:14
Moin Shirazi

The only change done to UINavigationBar API for iOS 11 is prefersLargetitles only. check here

You can do it to your own apps with one small change: check "Prefers Large Titles" for your navigation bar in IB, or if you prefer to do it in code using

navigationController?.navigationBar.prefersLargeTitles = true

Edit

If you need to change the text attributes of the large title you need to use the new largeTitleTextAttributes property on UINavigationBar:

UINavigationBar.appearance().largeTitleTextAttributes = [
    NSForegroundColorAttributeName: UIColor.white
]

Update for Swift 4.2:

As NSForegroundColorAttributeName has been renamed to NSAttributedString.Key.foregroundColor, use:

  UINavigationBar.appearance().largeTitleTextAttributes = [
      NSAttributedString.Key.foregroundColor: UIColor.black
  ]

UINavigationBar has a prefersLargeTitles: Bool property. Docs here.

class UINavigationBar {
   var prefersLargeTitles: Bool
}

UINavigationItem has a largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMode property. Docs here.

class UINavigationItem {
   var largeTitleDisplayMode: LargeTitleDisplayMode
}

Both of these can be modified in the Interface Builder.

To turn on this behavior set navigationController.navigationBar.prefersLargeTitles to true. Then you can control each individual view controller in the navigation controller stack by setting navigationItem.largeTitleDisplayMode.

The general design guidelines by Apple are that large titles shouldn't be used everywhere (for example, the Clock app does not use them), and it's generally preferred that only the first level of the navigation controller uses the large titles. However, these are just general guidelines.

Large titles are introduced in What's New in Cocoa Touch video (7:37).

if #available(iOS 11.0, *) {
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationItem.largeTitleDisplayMode = .always
} else {
    // Fallback on earlier versions
}

Note that there are some bugs in beta 1 which cause the large title to only appear when you manually scroll up.

Just check the "Prefers Large Titles" at Navigation Bar attribute inspector

if #available(iOS 11.0, *) {
    navigationController?.navigationBar.prefersLargeTitles = true
    navigationController?.navigationBar.topItem?.title = "Hello"
    navigationController?.navigationItem.largeTitleDisplayMode = .automatic

    let attributes = [
        NSAttributedStringKey.foregroundColor : UIColor.red,
        ]

    navigationController?.navigationBar.largeTitleTextAttributes = attributes
} else {
    // Fallback on earlier versions
}

Since large titles are available since iOS 11 you also have to check for iOS version:

    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = true
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!