My navigation bar's large title is too wide. How to fix that?

前端 未结 3 789
天命终不由人
天命终不由人 2021-02-15 09:07

I am using navigation controller, and I\'ve set to true its navigation bar\'s prefersLargeTitle property. Everything works fine, but when the text of my title becom

相关标签:
3条回答
  • 2021-02-15 09:59

    This question is somewhat answered here: How to resize Title in a navigation bar dynamically.

    self.title = "Your TiTle Text"
    let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
    tlabel.text = self.title
    tlabel.textColor = UIColor.whiteColor()
    tlabel.font = UIFont(name: "Helvetica-Bold", size: 30.0)
    tlabel.backgroundColor = UIColor.clearColor()
    tlabel.adjustsFontSizeToFitWidth = true
    self.navigationItem.titleView = tlabel
    

    That being said, this is slightly different, in that you have the prefersLargeTitle property set. Now, I am not sure whether the tlabel.adjustsFontSizeToFitWidth = true overrides the prefersLargeTitle property, but try it out and see if it works. There is also some additional information regarding navigation item large titles here: https://developer.apple.com/documentation/uikit/uinavigationitem/2909056-largetitledisplaymode. Hope this helps.

    0 讨论(0)
  • 2021-02-15 10:03

    This is the workaround that I found

    override func viewDidLoad() {
      super.viewDidLoad()
    
      title = yourTitle
      adjustLargeTitleSize()
    }
    
    extension UIViewController {
      func adjustLargeTitleSize() {
        guard let title = title, #available(iOS 11.0, *) else { return }
    
        let maxWidth = UIScreen.main.bounds.size.width - 60
        var fontSize = UIFont.preferredFont(forTextStyle: .largeTitle).pointSize
        var width = title.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: fontSize)]).width
    
        while width > maxWidth {
          fontSize -= 1
          width = title.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: fontSize)]).width
        }
    
        navigationController?.navigationBar.largeTitleTextAttributes =
            [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: fontSize)
        ]
      }
    }
    
    0 讨论(0)
  • 2021-02-15 10:03

    Made an edit to @vicente.fava answer - this works great.

    self.title = longTitle
    self.navigationController?.navigationBar.prefersLargeTitles = true
    adjustLargeTitleSize()
    
    
    extension UIViewController {
    func adjustLargeTitleSize() {
        guard let title = title, #available(iOS 11.0, *) else { return }
    
        let maxWidth = UIScreen.main.bounds.size.width - 60
        var fontSize = UIFont.preferredFont(forTextStyle: .largeTitle).pointSize
        var width = title.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)]).width
    
        while width > maxWidth {
            fontSize -= 1
            width = title.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)]).width
        }
    
        navigationController?.navigationBar.largeTitleTextAttributes =
            [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: fontSize)
        ]
    }
    

    }

    0 讨论(0)
提交回复
热议问题