I've got a UILabel placed inside a UINavigationBar.
I want to increase the font size of that label based on the height of the navigationBar
. When the navigationBar
is large, I want the font size to be bigger, and when I scroll and the navigationBar
shrinks, I want the font size to decrease.
What I've got so far (inside viewDidLoad):
dateLabel.text = "16. Oktober"
dateLabel.font = .boldSystemFont(ofSize: 40)
dateLabel.adjustsFontSizeToFitWidth = true
dateLabel.minimumScaleFactor = 0.5
dateLabel.backgroundColor = .white
dateLabel.numberOfLines = 1
dateLabel.lineBreakMode = .byTruncatingTail
self.navigationController?.navigationBar.addSubview(dateLabel)
dateLabel.translatesAutoresizingMaskIntoConstraints = false
dateLabel.bottomAnchor.constraint(equalTo: (self.navigationController?.navigationBar.bottomAnchor)!, constant: -16).isActive = true
dateLabel.leadingAnchor.constraint(equalTo: redView.trailingAnchor, constant: 16).isActive = true
dateLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
dateLabel.topAnchor.constraint(equalTo: (self.navigationController?.navigationBar.topAnchor)!, constant: 16).isActive = true
I set up constraints so that dateLabel
shrinks based on the height of the navigationBar
(which is working, as you can see in the screenshots).
However, the font size of dateLabel
does not change.
The screenshot shows the different states of the navigationBar
when I scroll.
What do I have to do to decrease the font size of the dateLabel
based on the height of the navigationBar
? Is this possible with auto layout?