Disable user interact in a view IOS

49,216

Solution 1

It's exactly the same, assuming your other view is either a member or you can iterate through self.view's array of subviews, like so:

MyViewController.h

UIView* otherView;

MyViewController.m

otherView.userInteractionEnabled = NO; // or YES, as you desire.

OR:

for (int i = 0; i < [[self.view subviews] count]; i++)
{
    UIView* view = [[self.view subviews] objectAtIndex: i];

    // now either check the tag property of view or however else you know
    // it's the one you want, and then change the userInteractionEnabled property.
}

Solution 2

In swift UIView do have property userInteractionEnabled to make it responsive or not. To make full View unresponsive use code:

// make screen unresponsive
self.view.userInteractionEnabled = false
//make navigation bar unresponsive
self.navigationController!.view.userInteractionEnabled = false

// make screen responsive
self.view.userInteractionEnabled = true
//make navigation bar responsive
self.navigationController!.view.userInteractionEnabled = true

Solution 3

for (UIView* view in self.view.subviews) {

    if ([view isKindOfClass:[/*"which ever class u want eg UITextField "*/ class]])

        [view setUserInteractionEnabled:NO];

}

hope it helps. happy coding :)

Solution 4

The best option is to use Tag property of the view rather than iterating all its subviews. Just set tag to the subView which you want to disable interaction and use below code to access it and disable interaction.

// considering 5000 is tag value set for subView 
// for which we want to disable user interaction  
UIView *subView = [self.view viewWithTag:5000]; 
[subView setUserInteractionEnabled:NO];
Share:
49,216
Newbee
Author by

Newbee

Nothing to say

Updated on July 18, 2022

Comments

  • Newbee
    Newbee almost 2 years

    I am disabling and enabling a view using the following code....

    [self.view setUserInteractionEnabled:NO];
    [self.view setUserInteractionEnabled:YES];
    

    If I do like this, all it subviews also got affected... All are disabled, how do I do only for particular view? Is it possible?