在视图 IOS 中禁用用户交互
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12213663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable user interact in a view IOS
提问by Newbee
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?
如果我这样做,所有它的子视图也会受到影响......所有都被禁用,我该如何只针对特定视图?是否可以?
回答by Luke
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:
完全相同,假设您的另一个视图是成员,或者您可以遍历self.view
的子视图数组,如下所示:
MyViewController.h
视图控制器.h
UIView* otherView;
MyViewController.m
视图控制器.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.
}
回答by Rahul Raina
In swift UIView
do have property userInteractionEnabled
to make it responsive or not. To make full View unresponsive use code:
在迅速UIView
确实有属性userInteractionEnabled
,使其响应与否。为了充分反应迟钝查看使用的代码:
// 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
回答by Anshuk Garg
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 :)
希望能帮助到你。快乐编码:)
回答by BornCoder
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.
最好的选择是使用Tag
视图的属性而不是迭代其所有子视图。只需将标记设置为要禁用交互的子视图,并使用以下代码访问它并禁用交互。
// 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];