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

Berry Blue
  • 15,330
  • 18
  • 62
  • 113

2 Answers2

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

Rahul_Chandnani
  • 275
  • 2
  • 9
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

dronpopdev
  • 797
  • 10
  • 13