0

I'l like for one of the buttons in my navigation bar to have a different action on longpress. If it were a normal button, I'd just add the gesture recognizer to its view, but a BarButton doesn't have one.

Clues?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190

1 Answers1

3

It's Painful process to add sub-view your gesture in UIBarButtonItem item. I show you in following code. It may help you.

I like to suggest you that instead of groping around for a subview you can create the button on your own and add a button bar item with a custom view

[[[yourToolbar subviews] objectAtIndex:[[yourToolbar items] indexOfObject:yourBarButton]] addGestureRecognizer:YourGesture];

// or also you can also try following....

[self.barButtonItem.customView addGestureRecognizer:longProcess];// i didn't declare longProcess and singletap GestureRecognizer here. I hope you can do that.
[self.barButtonItem.customView addGestureRecognizer:singletap];

Hope, this will help you..

Nitin
  • 7,455
  • 2
  • 32
  • 51
  • 4
    That assumes that the subview's index is always the same as the toolbar item's index which could easily break (or even crash) with any OS update. – omz May 06 '12 at 15:03
  • Well, the first one does not appear to be working, but I'm checking my assumptions and whether the gesture recognizer is in fact working as it should. – Anders Sewerin Johansen May 06 '12 at 15:12
  • Yep sorry for that but i didn't try it out as i have only short time but added one note in my answer. That may help to user. By the way thanks for point out mistake – Nitin May 06 '12 at 15:20
  • Hmm, I can drop an UIButton on the bar. This creates a UIBarButtonItem with the UIButton as a custom subview. Plus: I can use the easy way to add the gesture recognizer. Minus: The graphic looks out of place, as the toolbar translates the black to a nice grey for a bevelled effect. – Anders Sewerin Johansen May 06 '12 at 15:37
  • I marked the suggestion to use customView as the answer. You want to do the bit with adding an UIButton as the customView for this to work. – Anders Sewerin Johansen May 06 '12 at 17:02
  • yep..i was a;so thinking that. so i added it. thanks – Nitin May 06 '12 at 17:25