Skip to content

Instantly share code, notes, and snippets.

@grifas
Last active March 2, 2018 10:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grifas/5b49430e20a668a647c25701c1ea6ad1 to your computer and use it in GitHub Desktop.
Save grifas/5b49430e20a668a647c25701c1ea6ad1 to your computer and use it in GitHub Desktop.
iOS - Set a left title view in navigation bar
override func viewDidLoad() {
super.viewDidLoad()
// This keep the back button after setting the left title view
navigationItem.leftItemsSupplementBackButton = true
}
func setLeftTitleView() {
let titleLabel = UILabel()
titleLabel.text = "Left Title"
titleLabel.sizeToFit()
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: titleLabel.frame.size.width, height: 44))
titleView.addSubview(titleLabel)
// Sorry, I use Snapkit to set my constraints
titleLabel.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
let leftBarButton = UIBarButtonItem(customView: titleView)
navigationItem.setLeftBarButton(leftTitle, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment