Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recognize tap on navigation bar title

Could someone please help me recognise a tap when a user taps on the Title in the Navigation Bar?

I would like to recognise this tap and then animate the tableHeaderView appearing. Possibly sliding the TableView down.

The idea being that the user can then select a quick option (from the tableViewHeader) to re-populate the TableView.

However I cannot recognise any taps. I'm using Swift.

like image 728
Richard Avatar asked Oct 09 '14 20:10

Richard


People also ask

How do you set a navigation item title?

Discussion. Use navigationBarTitle(_:) to set the title of the navigation bar. This modifier only takes effect when this view is inside of and visible within a NavigationView .


1 Answers

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).

like image 151
rob mayoff Avatar answered Oct 06 '22 01:10

rob mayoff