Use the increased navigation-bar title in iOS 11

IosIphoneUinavigationbarXcode9 BetaIos11

Ios Problem Overview


iOS 11 Beta 1 uses the increased navigation-bar title for almost all system-apps (it started doing this in iOS 10 and the Music app). I am wondering if Apple has a public API for this coming in iOS 11, or whether it will stay private for now.

The behavior is that the title has an increased font-size, is left aligned and will move to the navigation-bar once the user scrolls down. I've attached some screens showing this behavior in the Messages app here:

enter image description here

Although I could not find any reference in the UINavigationController and UINavigationBar so far, maybe someone knows some more details!

Ios Solutions


Solution 1 - Ios

The only change done to UINavigationBar API for iOS 11 is prefersLargeTitles.

Documentation here: https://developer.apple.com/documentation/uikit/uinavigationbar/

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

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 = [
    NSAttributedString.Key.foregroundColor: UIColor.black
]

Solution 2 - Ios

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).

Solution 3 - Ios

Just check "Prefers Large Titles" in the Navigation Bar attribute inspector in Storyboard / Interface Builder:

enter image description here

Solution 4 - Ios

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
}

Solution 5 - Ios

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.

Solution 6 - Ios

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHans KnöchelView Question on Stackoverflow
Solution 1 - IosMoin ShiraziView Answer on Stackoverflow
Solution 2 - IoskgaidisView Answer on Stackoverflow
Solution 3 - IosMaorView Answer on Stackoverflow
Solution 4 - IosSai SandeepView Answer on Stackoverflow
Solution 5 - IosJordi BruinView Answer on Stackoverflow
Solution 6 - IosLavrin PristazhView Answer on Stackoverflow