Search code examples
ioseventstouchsuperview

Superview ignores UserInteractionEnabled = NO?


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!


Solution

  • 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.