Skip to content

Instantly share code, notes, and snippets.

@migueltg
Last active November 15, 2017 09:20
Show Gist options
  • Save migueltg/7779fe93aec48394a39cfdf6cbcfd99b to your computer and use it in GitHub Desktop.
Save migueltg/7779fe93aec48394a39cfdf6cbcfd99b to your computer and use it in GitHub Desktop.
Swift function for UINavigationItem extension to set title and subtitle in iOS navigation bar
extension UINavigationItem {
//Function to set title and subtitle in navigation bar
func setTitle(title:String, subtitle:String) {
let titleLabel = UILabel()
titleLabel.text = title
titleLabel.font = UIFont(name: "Roboto-Regular", size: 17)
titleLabel.textColor = UIColor.white
titleLabel.sizeToFit()
let subTitleLabel = UILabel()
subTitleLabel.text = subtitle
subTitleLabel.font = UIFont(name: "Roboto-Light", size: 14)
subTitleLabel.textColor = UIColor.white
subTitleLabel.textAlignment = .center
subTitleLabel.sizeToFit()
let stackView = UIStackView(arrangedSubviews: [titleLabel, subTitleLabel])
stackView.distribution = .equalCentering
stackView.axis = .vertical
let width = max(titleLabel.frame.size.width, subTitleLabel.frame.size.width)
stackView.frame = CGRect(x: 0, y: 0, width: width, height: 35)
titleLabel.sizeToFit()
subTitleLabel.sizeToFit()
self.titleView = stackView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment