Last active
March 2, 2018 10:43
-
-
Save grifas/5b49430e20a668a647c25701c1ea6ad1 to your computer and use it in GitHub Desktop.
iOS - Set a left title view in navigation bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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