Search code examples
iosobjective-cuitouch

Disable Touches on a view


Hi I have a UIView who's alpha is 0.7 and below it are some UITextFields. I don't want it to call touches events while keeping touches events. I tried using

[lightBoxView setUserInteractionEnabled:NO];

But now the UITextFields can become active or first responder. How can I disable it from calling touch events as well as not passing touches to others?


Solution

  • You also need to set the userInteractionEnabled = NO for all the subviews as well.

    Try this,

    [[lightBoxView subviews] makeObjectsPerformSelector:@selector(setUserInteractionEnabled:)
                              withObject:[NSNumber numberWithBool:NO]];
    

    This will call setUserInteractionEnabled: on all direct subviews of lightBoxView and set it to NO. For a more complex subview hierarchy you will have to recursively loop through all the child views and disable the user interaction on each one. For this you can write a recursive method in a UIView category. For more details about this category method take a look at this answer.

    Hope that helps!