2

How do I dynamically resize my navigation bar title while in iOS 11's Large Title mode?

Previously you could define your own UILabel as the titleView and have it resize. All it does now is add another titleView on top.

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "A very long title. Really long. Dont truncate me though. I want everyone to see me."
    let frame = CGRect(x: 0, y: 0, width: 200, height: 40)
    let tlabel = UILabel(frame: frame)
    tlabel.text = self.title
    tlabel.textColor = UIColor.black
    tlabel.font = UIFont.boldSystemFont(ofSize: 17) 
    tlabel.backgroundColor = UIColor.clear
    tlabel.adjustsFontSizeToFitWidth = true
    tlabel.textAlignment = .center
    self.navigationItem.titleView = tlabel

    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.largeTitleDisplayMode = .automatic
}
Nishant Bhindi
  • 2,242
  • 8
  • 21
meowmeowmeow
  • 713
  • 7
  • 17

0 Answers0