6

I have a subview thats goes onto the screen animated, and when it is on the screen, I want the superview (in my case, the view in the background), to ignore touch events. I tried everything but it does not work.

Is there any way to 'force' the superview to stop receiving touch events?

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

1 Answers1

4

When you say the superview, I assume you you mean the animating view's superview. Apple's documentation is very unspecific about userInteractionEnabled, but I think that if you set it to false, it disables touch events on the specific view, but not on its subviews. I would suggest that you do it recursively. Here is an example of code that you could use to disable/enable ALL touch events on a view:

- (void)setInteraction:(BOOL)allow onView:(UIView *)aView {
    [aView setUserInteractionEnabled:allow];
    for (UIView * v in [aView subviews]) {
        [self setInteraction:allow onView:v];
    }
}

You could then call this on your superview [self setInteraction:NO onView:[self superview]]. This would, of course, disable your touch events as well, since you are disabling them recursively on your superview. Of course you can always re-enable your touch events [self setUserInteractionEnabled:NO].

Also, Apple's UIView Class Reference mentions that some UI components override this method:

Note: Some UIKit subclasses override this property and return a different default value. See the documentation for any class you use to determine if it returns a different value for this property.

Alex Nichol
  • 7,512
  • 4
  • 32
  • 30
  • 1
    I'm pretty sure that userInteractionEnabled affects all subviews as well. – Dancreek Aug 06 '11 at 18:00
  • Are you sure? I just need it on the superview and thats it. – SimplyKiwi Aug 06 '11 at 18:05
  • 1
    This answer seems to clear up whether child views obey their parents' userInteractionEnabled setting: http://stackoverflow.com/questions/4661589/how-to-get-touches-when-parent-view-has-userinteractionenabled-set-to-no-in-ios – Dave Cameron Apr 02 '12 at 18:50