4

I have the following setup in my iPhone project (a UIButton that has a UIView as sub-view):

- UIButton       // super-view   <-------|
  |                                <touch-event>
  |_ UIView      // sub-view     ________|

I have connected an action (touch-up-inside) from my UIButton with IB to my view controller. But unfortunately the UIView prevents the OS to call the action method of my UIButton, since the UIView subview does not respond to touches and thus not forward to its super-view.

How can I forward the action event from UIView to the super-view UIButton ? So that the UIButton "thinks" that it has been touched although the sub-view was touched.

I already found these links, but they didn't help me:

Some code sample in Objective-C would be great!

Community
  • 1
  • 1
salocinx
  • 3,715
  • 8
  • 61
  • 110
  • Try self.view.layer.zPosition = 1; may be helped.... – Joe Hallenbeck Aug 26 '15 at 11:00
  • You mean to set the UIView layer's zPosition to 1 ? That didn't work. But thanks for your response. – salocinx Aug 26 '15 at 11:10
  • Give me screen with layers hierarchy, please, and i means self.button.layer.zPosition = 1; – Joe Hallenbeck Aug 26 '15 at 11:12
  • What exactly do you want to accomplish by using an UIView as a subview of an UIButton? How about layering a (semi) transparent UIButton on top of your UIView instead? – Markus Dheus Aug 26 '15 at 11:25
  • Hm.. does also not work with ´self.button.layer.zPosition = 1;´. I combine the views programmatically. Instantiating a new ´UIButton´ and ´UIView´ and then a the ´UIView´ to the ´UIButton´ with ´[UIButton addSubview:UIView]´. – salocinx Aug 26 '15 at 11:26
  • @Markus Dheus: I am using a customized subclass of UIButton with a flip effect. The UIView is for displaying a tinted image in which I override the drawInRect: method. Here's the entire class I have built up: http://stackoverflow.com/questions/32123244/ios-how-to-animate-a-flip-between-two-uiviews – salocinx Aug 26 '15 at 11:29
  • @salocinx if U add subview to button with addSubview method, try to use another method for example - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; or - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; | i think it is helped to you. – Joe Hallenbeck Aug 26 '15 at 11:37
  • Is the exclusiveTouch property of your UIView set to NO? – Markus Dheus Aug 26 '15 at 11:37
  • Yes the exclusiveTouch property of the UIView is set to NO. But the problem is, I think, that UIView isn't a responder at all..?! – salocinx Aug 26 '15 at 12:04
  • @Joe Hallenbeck: The insert-variant does not make much sense, since its use is for the case you have multiple sub-views, but in my case, I only have a single sub-view. But thanks anyway. – salocinx Aug 26 '15 at 12:17
  • If you have looked at other questions and they do not resolve your problem, please be clear about what you have tried, what the results of trying were, and how this differs from what you need. – nhgrif Aug 26 '15 at 12:23
  • 1
    I can't try this myself at the moment, but have you tried overriding `(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event` in your UIView and returning always NO as an answer? – Markus Dheus Aug 26 '15 at 12:59
  • @Markus Dheus: Great - this did it :-) Please make an answer with your comment and I will accept it. Thank you very much! – salocinx Aug 26 '15 at 13:06
  • Glad it worked out and cheers for suggesting to add it as an answer :-) – Markus Dheus Aug 26 '15 at 13:09

1 Answers1

4

Overriding (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event in your UIView and always returning NO should do the trick.

Markus Dheus
  • 576
  • 2
  • 8