我正在使用以下代码禁用和启用视图....
[self.view setUserInteractionEnabled:NO];
[self.view setUserInteractionEnabled:YES];
如果我这样做,所有子视图也会受到影响...所有都被禁用,我该怎么做只针对特定视图?有可能吗?
答案 0 :(得分:32)
它完全相同,假设您的其他视图是成员,或者您可以遍历self.view
的子视图数组,如下所示:
<强> 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.
}
答案 1 :(得分:12)
在swift DEBUG
中,确实有UIView
属性以使其响应或不响应。要使完整的View无响应使用代码:
userInteractionEnabled
答案 2 :(得分:5)
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[/*"which ever class u want eg UITextField "*/ class]])
[view setUserInteractionEnabled:NO];
}
希望它有所帮助。快乐的编码:)
答案 3 :(得分:1)
最好的选择是使用视图的Tag
属性,而不是迭代其所有子视图。只需将标记设置为要禁用交互的subView,并使用下面的代码访问它并禁用交互。
// 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];