Skip to content

Instantly share code, notes, and snippets.

@achernoprudov
Created October 28, 2020 05:38
Show Gist options
  • Save achernoprudov/ed2cd9583998422bc92838ca30ae2b42 to your computer and use it in GitHub Desktop.
Save achernoprudov/ed2cd9583998422bc92838ca30ae2b42 to your computer and use it in GitHub Desktop.
UINavigationItem with subtitle
extension UINavigationItem {
func setTitle(title: String, subtitle: String) {
let one = UILabel()
one.text = title
one.font = UIFont.systemFont(ofSize: 17)
one.sizeToFit()
let two = UILabel()
two.text = subtitle
two.font = UIFont.systemFont(ofSize: 12)
two.textAlignment = .center
two.sizeToFit()
let stackView = UIStackView(arrangedSubviews: [one, two])
stackView.distribution = .equalCentering
stackView.axis = .vertical
stackView.alignment = .center
let width = max(one.frame.size.width, two.frame.size.width)
stackView.frame = CGRect(x: 0, y: 0, width: width, height: 35)
one.sizeToFit()
two.sizeToFit()
self.titleView = stackView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment