Last active
July 20, 2023 11:58
-
-
Save nazywamsiepawel/0166e8a71d74e96c7898 to your computer and use it in GitHub Desktop.
UINavigationBar with subtitle / swift
I joined @odonckers and @blinkelvin code together
func setTitle(title: String, titleColor: UIColor, titleSize: Int, subtitle: String, subtitleColor: UIColor, subtitleSize: Int , view: UIView) -> UIView {
let titleLabel = UILabel(frame: CGRect(x:0, y:-5, width: view.frame.width - 100, height: 20))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = titleColor
titleLabel.adjustsFontSizeToFitWidth = false
titleLabel.font = UIFont.boldSystemFont(ofSize: CGFloat(titleSize))
titleLabel.lineBreakMode = .byTruncatingTail
titleLabel.textAlignment = .center
titleLabel.text = title
let subtitleLabel = UILabel(frame: CGRect(x:0, y:18, width: view.frame.width - 100, height: 10))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = subtitleColor
subtitleLabel.adjustsFontSizeToFitWidth = false
subtitleLabel.lineBreakMode = .byTruncatingTail
subtitleLabel.textAlignment = .center
subtitleLabel.font = UIFont.systemFont(ofSize: CGFloat(subtitleSize))
subtitleLabel.text = subtitle
let titleView = UIView(frame: CGRect(x:0, y:0, width: view.frame.width - 30, height:30))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
return titleView
}
here is an example of how to use it
self.navigationItem.titleView = setTitle(title: "TITLE", titleColor: UIColor.black, titleSize: 16, subtitle: "SUBTITle", subtitleColor: UIColor.gray, subtitleSize: 12, view: self.view)
I notice it looks very good with Regular NavigationBar
but with Large NavigationBar it stays on the same size
it needs to improve with Large Navigation bar
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works great! One question is how can this go under the left bar button instead of beside it?