2

Here is a question for you:

I've got a UIButton that we'll call bottomButton. I have another UIButton that we'll call topButton. What I want to do is place topButton on top of bottomButton such that when you tap topButton, it's action is executed. The problem that I'm having is that when I tap topButton, its bottomButton's action that is getting executed.

So what I want to know is if it is possible to place a UIButton on another UIButton so that when tapped, only the top UIButton registers the tap. Savvy?

Any input is appreciated!

hemlocker
  • 1,012
  • 1
  • 9
  • 24

5 Answers5

3

Yes, it's possible. There are three things to check out:

1) Is topButton actually situated above bottomButton in the view stack?
  [UIView bringSubviewToFront:] may help here.
2) Is topButton large enough to register the tap?
  Try making topButton larger than 44x44 and see if that helps.
3) Make sure that topButton is enabled.

EDIT

I just saw your comment about adding topButton as a subview of bottomButton, and that's likely to be part of the problem. You could try adding both bottomButton and topButton to a new UIView, and then add that new UIView where you had previously used bottomButton.

Mike Hay
  • 2,828
  • 21
  • 26
  • Alright. So if both `topButton` and `bottomButton` are subviews of this new UIView, how do I reference `bottonButton` from `topButton`'s action? I wouldnt be able to just use `superview` anymore. – hemlocker Aug 31 '11 at 18:28
  • 2
    The absolute simplest way would be to set the `tag` value on `bottomButton` and use `[superview viewWithTag:]` from topButton's action method to find it. – Mike Hay Aug 31 '11 at 18:30
  • awesome! I didn't know there was a `viewWithTag:` method – hemlocker Aug 31 '11 at 18:43
3

If top button is a subview of bottom it won't get the touch events passed to it because the bottom button's hitTest:withEvent: is called first and it will return YES.

You could try overriding hitTest:withEvent: for the bottom buttom to have it call hitTest on its subviews (normally it doesn't have UIControls in its subviews) and return that subview instead if it has a hit.

progrmr
  • 75,956
  • 16
  • 112
  • 147
2

Ah, I see now from reading your comments. Since you're adding a button TO another button, you'll need to make the superview intercept the touch event.

Read this post: How to make a superview intercept button touch events?

Community
  • 1
  • 1
Chase Henslee
  • 3,918
  • 1
  • 18
  • 21
0

I'm not sure about this one but have you tried adding them to the view in different orders? Does that make a difference? Test it out and see what happens!

Rupert Horlick
  • 671
  • 5
  • 15
  • I'm not adding them both to the view. I'm adding `bottonButton` to the view, and then `topButton` to `bottomButton` – hemlocker Aug 31 '11 at 18:15
0

Why would you add a button to a button?

Try adding them to the same parent but putting topButton over bottomButton.

Pontus Granström
  • 1,089
  • 8
  • 18
  • Because I want a tap on `topButton` to make a change to `bottomButton` and when one is a superview of the other, it makes the job easier. – hemlocker Aug 31 '11 at 18:22
  • If your superview knows about the two buttons, it can change one when you click on the other. That's a cleaner way to do it IMHO. – Pontus Granström Aug 31 '11 at 18:34