How to resize Title in a navigation bar dynamically

前端 未结 9 1906
慢半拍i
慢半拍i 2020-12-03 01:24

I have some views that show up in a navigation controller. Two of these views have a longer title for the navigation bar.

The problem is that when the title is too

相关标签:
9条回答
  • 2020-12-03 02:15

    Swift 5 and iOS 13 / iOS 14

    The answers from above don't work if you have a large title in Swift 5 and iOS 13 because they simply add another title to your navigation bar. Instead you could use the largeTitleTextAttributes property (available since iOS 11) to shrink your title when needed.
    Assuming you have set your large title via storyboard or code already, you can use the following method:

    private func configureNavigationTitle(_ title: String) {
            let tempLabel = UILabel()
            tempLabel.font = UIFont.systemFont(ofSize: 34, weight: .bold)
            tempLabel.text = title
    
            if tempLabel.intrinsicContentSize.width > UIScreen.main.bounds.width - 30 {
                var currentTextSize: CGFloat = 34
                for _ in 1 ... 34 {
                    currentTextSize -= 1
                    tempLabel.font = UIFont.systemFont(ofSize: currentTextSize, weight: .bold)
                    if tempLabel.intrinsicContentSize.width < UIScreen.main.bounds.width - 30 {
                        break
                    }
                }
                navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: currentTextSize, weight: .bold)]
            }
            self.title = title
        }
    

    So essentially we are ussing a helper label in order to get the width of our title and then we are going to shrink the font size until the title fits in our navigation bar. Call it from viewDidLoad():

    override func viewDidLoad() {
            super.viewDidLoad(
            configureNavigationTitle("A very long title which fits perfectly fine")
        }
    
    0 讨论(0)
  • 2020-12-03 02:21

    In case you have a view added into titleView, and you want to resize the view, you can use this code (Swift 3):

    self.translatesAutoresizingMaskIntoConstraints = false
    self.layoutIfNeeded()
    self.sizeToFit()
    self.translatesAutoresizingMaskIntoConstraints = true
    
    0 讨论(0)
  • Here's an example in Swift that also allows for multiple lines. Using PureLayout to simplify auto layout.

    override func viewDidLoad() {
      super.viewDidLoad()
      configureTitleView()
    }
    
    func configureTitleView() {
      let titleLabel = UILabel()
      titleLabel.numberOfLines = 0
      titleLabel.textAlignment = .Center
      titleLabel.font = UIFont.boldSystemFontOfSize(17.0)
      titleLabel.text = searchLoc.mapItem.name
      navigationItem.titleView = titleLabel
      titleLabel.autoPinEdgesToSuperviewMargins() // PureLayout method
      titleLabel.adjustsFontSizeToFitWidth = true
    }
    

    And a usage example:

    enter image description here

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