How do I turn off large titles for UINavigationBar?
Asked Answered
A

9

24

I have a UITableView and a Detail View embedded in a UINavigationController as so: enter image description hereI would like to turn on large titles for "My Notes" but I'd like to turn it off for the detail view. Something like how the default Mail app works on iPhone. How would I change the navigation bar's prefersLargeTitle property during that segue?

Allhallowmas answered 20/12, 2017 at 14:17 Comment(0)
P
31

it's very simple.

In your DetailView you should set navigationItem.largeTitleDisplayMode to .never

(not navigationController?.navigationItem.largeTitleDisplayMode !!)

navigationItem.largeTitleDisplayMode = .never
Pilsen answered 6/10, 2018 at 11:21 Comment(0)
T
22

SwiftUI version

.navigationBarTitle("Title", displayMode: .inline)

Update for iOS 14 and later:

.navigationTitle("Details")
.navigationBarTitleDisplayMode(.inline)
Tunisia answered 25/9, 2019 at 15:49 Comment(1)
For future iOS releases, this method will be removed. The following method should be used instead: .navigationBarTitleDisplayMode(.inline). Plus the navigation title could be set with .navigationTitle("Details")Festschrift
K
6

Any one of both of following, will solve your problem:

  1. set prefersLargeTitles to false for your navigationBar

    self.navigationController?.navigationBar.prefersLargeTitles = false
    
  2. set largeTitleDisplayMode to never for navigationItem (note: prefersLargeTitles must be false otherwise this won't work)

    self.navigationController?.navigationItem.largeTitleDisplayMode = .never
    

Note: if prefersLargeTitles is true, then largeTitleDisplayMode = .never won't work. Small title display for navigation bar is dependent on prefersLargeTitles

This will enable large title mode if it's value is true

self.navigationController?.navigationBar.prefersLargeTitles = true
Khania answered 20/12, 2017 at 14:32 Comment(3)
This is just incorrect. prefersLargeTitles doesn't have to be false for #2. And largeTitleDisplayMode = .never will definitely work when prefersLargeTitles is true.Judah
It is also important to note that this is only available in iOS 11+.Bend
Just wanted to note that in my case #1 and #2 both needed (just #2 was not enough), even when prefer large title flag was set to "Never" in navigationItem's settings in storyboard.Moniliform
L
6

I had the same issue just now.

My use case:

MasterVC: basic navigation bar without largeTitle

DetailVC: largeTitle enabled

--> When going back to the MasterVC from the DetailVC I was seeing a weird animation which showed a largeTitle on the Master for a sec before going back to the basic non largeTitle layout. It looked like a glitch.

I fixed it by following this approach:

In MasterVC - viewDidLoad

if #available(iOS 11.0, *) {
     navigationItem.largeTitleDisplayMode = .never
     navigationController?.navigationBar.prefersLargeTitles = false
}

In DetailVC - viewDidLoad

if #available(iOS 11.0, *) {
     navigationItem.largeTitleDisplayMode = .always
     navigationController?.navigationBar.prefersLargeTitles = true
} 

I hope that can help others.

Lockyer answered 23/6, 2018 at 10:18 Comment(0)
V
6

It should be noted that if you set largeTitleDisplayMode to never, and prefersLargeTitles to false on a detail ViewController, the small title will continue to display for a second when moving from the detail ViewController to the previous ViewController via the UINavigationBar back button.

Use willMove(toParent:) function to change the title back before the segue is performed.

Swift 4

override func willMove(toParent parent: UIViewController?) {
    navigationItem.largeTitleDisplayMode = .always
    navigationController?.navigationBar.prefersLargeTitles = true
}
Versicular answered 23/1, 2019 at 3:3 Comment(0)
A
5
    if #available(iOS 11.0, *) {
        self.navigationItem.largeTitleDisplayMode = UINavigationItem.LargeTitleDisplayMode.never
    } else {
        // Fallback on earlier versions
    }
Allhallowmas answered 20/12, 2017 at 14:25 Comment(3)
You should provide more context around your answer. Where is this code going? What is it doing?Grati
@NicolasGrenié, the code is pretty clear because properties in iOS are self-descriptive. Your comment, in turn seems to be left in hurry or even generated by a bot.Erogenous
UINavigationItemLargeTitleDisplayModeNever also works, since the implication here is that you don't have access to .never. @NicolasGrenié You might want to consider some basic tutorials before worrying about title bar size.Bend
L
4

It might be very late but this could be useful for someone..

include the below code on your detail view controller under viewDidLoad

navigationItem.largeTitleDisplayMode = .never
Lubow answered 30/3, 2019 at 10:59 Comment(0)
H
2

SwiftUI:

step 1. Use ZStack step 2 : .navigationBarTitle("", displayMode: .inline)

Habakkuk answered 3/3, 2021 at 16:45 Comment(0)
J
1

I had the same issue and needed to place a NavigationItem on the second ViewController's storyboard. My NavigationItem was being created automatically by the segue and its prefersLargeTitle in the viewDidLoad() was not finished loading before the view appeared. Adding a NavigationItem to the storyboard fixed this issue and allowed me to set the prefersLargeTitle in the storyboard's properties menu.

Jocular answered 26/1, 2020 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.