How to disable touch input to all views except the top-most view?

10 Solutions Collect From Internet About “How to disable touch input to all views except the top-most view?”

Hope this help…

[[yourSuperView subviews]
   withObject:[NSNumber numberWithBool:FALSE]];

which will disable userInteraction of a view’s immediate subviews..Then give userInteraction to the only view you wanted

yourTouchableView.setUserInteraction = TRUE;


It seems in iOS disabling userInteraction on a parent view doesn’t disable userInteraction on its childs.. So the code above (I mean the one with makeObjectsPerformSelector:)will only work to disable userInteraction of a parent’s immediate subviews..

See user madewulf’s answer which recursively get all subviews and disable user interaction of all of them. Or if you need to disable userInteraction of this view in many places in the project, You can categorize UIView to add that feature.. Something like this will do..

@interface UIView (UserInteractionFeatures)

@implementation UIView(UserInteractionFeatures)
    self.userInteractionEnabled =   value;
    for (UIView *view in [self subviews]) {
        [view setRecursiveUserInteraction:value];

Now you can call

[yourSuperView setRecursiveUserInteraction:NO];

Also user @lxt’s suggestion of adding an invisible view on top of all view’s is one other way of doing it..

There are a couple of ways of doing this. You could iterate through all your other subviews and set userInteractionEnabled = NO, but this is less than ideal if you have lots of other views (you would, after all, have to subsequently renable them all).

The way I do this is to create an invisible UIView that’s the size of the entire screen that ‘blocks’ all the touches from going to the other views. Sometimes this is literally invisible, other times I may set it to black with an alpha value of 0.3 or so.

When you expand your main subview to fill the screen you can add this ‘blocking’ UIView behind it (using insertSubview: belowSubview:). When you minimize your expanded subview you can remove the invisible UIView from your hierarchy.

So not quite built-in, but I think the simplest approach. Not sure if that was what you were thinking of already, hopefully it was of some help.

Beware of the code given as solution here by Krishnabhadra:

[[yourSuperView subviews]makeObjectsPerformSelector:@selector(setUserInteractionEnabled:) withObject:[NSNumber numberWithBool:FALSE]];

This will not work in all cases because [yourSuperView subviews] only gives the direct subviews of the superview. To make it work, you will have to iterate recursively on all subviews:

-(void) disableRecursivelyAllSubviews:(UIView *) theView
    theView.userInteractionEnabled = NO;
    for(UIView* subview in [theView subviews])
        [self disableRecursivelyAllSubviews:subview];

-(void) disableAllSubviewsOf:(UIView *) theView
    for(UIView* subview in [theView subviews])
        [self disableRecursivelyAllSubviews:subview];

Now a call to disableAllSubviewsOf will do what you wanted to do.

If you have a deep stack of views, the solution by lxt is probably better.

I would do this by putting a custom transparent button with the same frame as the superView. And then on top of that button I would put view that should accept user touches.
Button will swallow all touches and views behind it wouldn’t receive any touch events, but view on top of the button will receive touches normally.

Something like this:

- (void)disableTouchesOnView:(UIView *)view {
    UIButton *ghostButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    [ghostButton setBackgroundColor:[UIColor clearColor]];
    ghostButton.tag = 42; // Any random number. Use #define to avoid putting numbers in code.

    [view addSubview:ghostButton];

And a method for enabling the parentView.

- (void)enableTouchesOnView:(UIView *)view {
    [[view viewWithTag:42] removeFromSuperview];

So, to disable all views in the parentViev behind yourView, I would do this:

YourView *yourView = [[YourView alloc] initWithCustomInitializer];
// It is important to disable touches on the parent view before adding the top most view.
[self disableTouchesOnView:parentView];
[parentView addSubview:yourView];

Just parentView.UserInteractionEnabled = NO will do the work.
Parent view will disable user interaction on all the view’s subviews. But enable it does not enable all subviews(by default UIImageView is not interactable). So an easy way is find the parent view and use the code above, and there is no need to iterate all subviews to perform a selector.

setUserInteractionEnabled = NO on the view you want to disable

I will give my 2 cents to this problem.
Iteratively run userInteractionEnabled = false it’s one way.
Another way will be add a UIView like following.


#import <UIKit/UIKit.h>
@interface EZEventEater : UIView


#import "EZEventEater.h"
@implementation EZEventEater

- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = false;
    return self;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
   //EZDEBUG(@"eater touched");

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event


In your code you add the EZEventEater view to cover all the views that your may block the touch event.
Whenever you want to block the touch event to those views, simply call

eater.userInteractionEnabled = YES;

Hope this helpful.

Add a TapGestureRecognizer to your “background view” (the translucent one which “grays out” your normal interface) and set it to “Cancels Touches In View”, without adding an action.

let captureTaps = UITapGestureRecognizer()
captureTaps.cancelsTouchesInView = true

For my app, I think it will be sufficient to disable navigation to other tabs of the app (for a limited duration, while I’m doing some processing):

self.tabBarController.view.userInteractionEnabled = NO;

Also, I disabled the current view controller–

self.view.userInteractionEnabled = NO;

(And, by the way, the recursive solutions proposed here had odd effects in my app. The disable seems to work fine, but the re-enable has odd effects– some of the UI was not renabled).

I had the same problem, but the above solutions did not help.
I then noticed that calling
super.touchesBegan(...) was the problem.
After removing this the event was only handled by the top-most view.

I hope this is of help to anybody.