I'm trying to place a UIButton as the titleView for my UINavigationItem. This is my code but it doesn't work. The title is completely blank.
override func viewDidLoad() {
super.viewDidLoad()
let titleButton = UIButton()
titleButton.titleLabel?.font = UIFont(name: "HelveticaNeue-Medium", size: 16)
titleButton.titleLabel?.textAlignment = NSTextAlignment.Center;
titleButton.titleLabel?.text = "My Custom Title"
titleButton.titleLabel?.sizeToFit()
self.navigationItem.titleView = titleButton
}
Here is a screenshot of the missing title.

If you are using Storyboards, you can drag a UIButton to the titleView. If Interface Builder doesn't let you do it, it's because you need to drag first a UINavigationItem to the navigation bar, so you can then drag views inside, like your UIButton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With