Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable user interact in a view IOS

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?

like image 980
Newbee Avatar asked Aug 31 '12 10:08

Newbee


4 Answers

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.
}
like image 167
Luke Avatar answered Nov 20 '22 16:11

Luke


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
like image 20
Rahul Raina Avatar answered Nov 20 '22 17:11

Rahul Raina


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 :)

like image 5
Anshuk Garg Avatar answered Nov 20 '22 16:11

Anshuk Garg


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];
like image 1
BornCoder Avatar answered Nov 20 '22 16:11

BornCoder