1

Is it possible to detect a touch in a UINavigationItem?

I would like to create a little animation on an UINavigationItem when the user taps on it.

In other words:

  • my titles contain way too much text, hence I had to truncate them
  • I want the user to be able to read the full title text when he/she presses the navigation item
  • To do so I need to:

    1. Detect the touch
    2. Animate the String / text (I could have a timer to animate this, however I am wondering if there is already a built in function in iOS - do you know of any?)
shallowThought
  • 19,212
  • 9
  • 65
  • 112
mm24
  • 9,280
  • 12
  • 75
  • 170
  • UINavigationItem is not a UIView subclass, it inherits straight from NSObject (as shown in https://developer.apple.com/reference/uikit/uinavigationitem?language=objc ). Therefore you can not press it as it is not a visible element of the UI. – johnyu Jan 16 '17 at 13:21
  • Please add a tag for the language you are using. Related questions that might help: http://stackoverflow.com/questions/26287322/recognize-tap-on-navigation-bar-title and http://stackoverflow.com/questions/2077025/uinavigationbar-touch – shallowThought Jan 16 '17 at 13:27

2 Answers2

0

UINavigationItem inherits directly from NSObject as johnryu stated in the comments, but instead of showing a title in your navigation bar you can do something more complex by adding a UIView in place of the title.
This view can be composed of a UILabel that will show the actual title and a transparent button.
The transparent button can have an action that triggers a popup with a full text for a "touch down" event or "touch up inside".
Or you can add to that view a simple tap gesture recognizer.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

Try assigning a custom titleView to UINavigationItem. Make it a UIView with a label inside which you will have your text. In default state, the label would have same size as its superview. Make sure that the UIView has clipsToBounds set to YES/true.

Attach a UITapGestureRecognizer to either the label or the view to switch to "animating" state and back.

When entering animating state, make the label wide enough to have enough space for the entire string and animate its movement within the container, you can use NSString's boundingRectWithSize:options:attributes:context: to calculate the proper width.

Then just add an animation where label moves to the left until its right end is visible, probably best if it's repeating.

johnyu
  • 2,152
  • 1
  • 15
  • 33