在大标题模式下动态调整导航栏标题的大小

时间:2017-10-17 11:51:25

标签: ios swift ios11 navigationbar

如何在iOS 11的大标题模式下动态调整导航栏标题的大小?

Previously您可以将自己的UILabel定义为titleView并调整大小。它现在所做的就是在顶部添加另一个titleView。

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
}

0 个答案:

没有答案