Multiple lines for large titles in navigation bars in iOS 11
Asked Answered
N

2

17

Is it possible to have the new large titles for navigation bars in iOS 11 show multiple lines? The App Store app does this but I can't find anything in the current documentation to do this. The standard behavior just shows one line with ellipsis if it's too long.

enter image description here

Nore answered 5/10, 2017 at 5:43 Comment(2)
did you get any solution for this ?Nuclei
If it can help here's what I did a another thread : https://mcmap.net/q/746741/-how-to-line-break-long-large-title-in-ios-11Musetta
D
5

Add following code into viewWillAppear:

    navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationController?.navigationItem.largeTitleDisplayMode = .automatic

    self.title = "Hello big text, For navigation large style bar"
    navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font : UIFont.preferredFont(forTextStyle: .largeTitle)]

    var count = 0
    for item in(self.navigationController?.navigationBar.subviews)! {
        for sub in item.subviews{
            if sub is UILabel{
                if count == 1 {
                    break;
                }
                let titleLab :UILabel = sub as! UILabel
                titleLab.numberOfLines = 0
                titleLab.text = self.title
                titleLab.lineBreakMode = .byWordWrapping
                count = count + 1
            }
        }

    }
    self.navigationController?.navigationBar.layoutSubviews()
    self.navigationController?.navigationBar.layoutIfNeeded()

Facing issue with back button will update soon..

Dragrope answered 20/12, 2017 at 13:35 Comment(4)
Any update for the back button issue? I can only get it to work in the viewdidappear, which only applies after the view is visible and thus looks blocky.Daynadays
maybe you can just use break where you've assigned the count.Verger
this does not work, the label goes multiline but nav bar does not increase its height @DragropeNarvaez
Agreed with @ShahzaibQureshiPloch
S
1

There is a way to do this simply by using a non-public API. Use at your own risk:

class ViewController: UIViewController {

   override func viewDidLoad() {
      super.viewDidLoad()
      title = "Thunderbox Entertaiment"
      navigationItem.enableMultilineTitle()
   }

}
extension UINavigationItem {
   
   func enableMultilineTitle() {
      setValue(true, forKey: "__largeTitleTwoLineMode")
   }
   
}

Result: enter image description here

Sassenach answered 20/1, 2022 at 19:48 Comment(1)
private api may lead to unpredictable results like crashes and review rejectionGynaecology

© 2022 - 2024 — McMap. All rights reserved.