UINavigationBar
does not expose its internal view hierarchy. There is no supported way to get a reference to the UILabel
that displays the title.
You could root around in its view hierarchy “manually” (by searching through its subviews
), but that might stop working in a future iOS release because the view hierarchy is private.
One workaround is to create a UILabel
and set it as your view controller's navigationItem.titleView
. It's up to you to match the style of the default label, which may change in different versions of iOS.
That said, it's pretty easy to set up:
override func didMove(toParentViewController parent: UIViewController?) {
super.didMove(toParentViewController: parent)
if parent != nil && self.navigationItem.titleView == nil {
initNavigationItemTitleView()
}
}
private func initNavigationItemTitleView() {
let titleView = UILabel()
titleView.text = "Hello World"
titleView.font = UIFont(name: "HelveticaNeue-Medium", size: 17)
let width = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
titleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))
self.navigationItem.titleView = titleView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.titleWasTapped))
titleView.userInteractionEnabled = true
titleView.addGestureRecognizer(recognizer)
}
@objc private func titleWasTapped() {
NSLog("Hello, titleWasTapped!")
}
I'm setting the size of the label to its natural width (using sizeThatFits:
), but I'm setting its height to 500. The navigation bar will keep the width but shrink the height to the bar's own height. This maximizes the area available for tapping (since the natural height of the label might be only ~22 points but the bar is 44 points high).