1

I have a toolbar with 5 buttons.

4 of them are regular bar button items and one of them is a custom one (A 'UIButton' inside a 'UIBarButtonItem').

I noticed that when I click between the regular buttons (not exactly on them), one of them (the closest one) still recieves the click event and is being highlighted (which is what I want).

But the custom bar button item does not show this behaviour.

When I tap between it and one of the regular buttons neither of the 2 receives the touch event. This probably because the UIButton is the one the gets the click event. Is there a way to add a touch event the containing bar button item as well? Or perhaps another way to solve this?

Thanks!

Dustin
  • 6,783
  • 4
  • 36
  • 53
Omri Perl
  • 131
  • 2
  • 8
  • Have you checked if there are any `UITapGestureRecognizer`s stealing the touches? It happened to me before. If that's the case, you have to check this: http://stackoverflow.com/questions/3344341/uibutton-inside-a-view-that-has-a-uitapgesturerecognizer – Ravi Aug 08 '12 at 14:56
  • You are right, sorry about that :) – Omri Perl Aug 09 '12 at 07:31

3 Answers3

2

button.userInteractionEnabled = YES; I believe is the answer.

Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
  • Thanks, but I don't really see why is that a solution to my problem. It is not even related to the question (Perhaps my question was unclear).. – Omri Perl Aug 09 '12 at 07:35
  • All of your components are subclasses of `UIView`. If `parentView.userInteractionEnabled = NO` then the child views won't receive the touch message. Hence my solution. Did you try it, by the way? – Eric Brotto Aug 09 '12 at 13:30
  • The button interaction is enabled already. the problem is imitating the default behaviour of clicking between toolbar buttons and still getting a click on one of them – Omri Perl Aug 13 '12 at 07:09
1

One solution might be that you create image of same as bar button item and assign to UIButton as background image, this will solve your issue.

Hope this helps you....

P.J
  • 6,547
  • 9
  • 44
  • 74
  • Yep, that's a workaround. I thought about doing that but I don't think that I want to add additional images.. But it seems to me like what you suggest is the only way to go... – Omri Perl Aug 09 '12 at 07:34
  • I think I got it! I only added the touch event to the UIButton(Inside the bar button item). Perhaps I should also add the touch event (To the same function the UIButton references to) to the containing UIBarButtonItem. I'll try it and let you know of the result! – Omri Perl Aug 09 '12 at 07:38
0

Solved it! I added another UIView to the UIBarButtonItem and then I added the UIButton to the UIView. I added a touch gesture to the UIView (And also kept the original touch event of the UIButton) that expanded the area that you can touch the button which solved the problem.

Omri Perl
  • 131
  • 2
  • 8