Iphone 禁用视图中的用户交互
我正在使用以下代码禁用和启用视图Iphone 禁用视图中的用户交互,iphone,objective-c,ios,xcode,cocoa-touch,Iphone,Objective C,Ios,Xcode,Cocoa Touch,我正在使用以下代码禁用和启用视图 [self.view setUserInteractionEnabled:NO]; [self.view setUserInteractionEnabled:YES]; 如果我这样做,所有it子视图也会受到影响。。。所有视图都已禁用,如何仅针对特定视图执行操作?可能吗 for (UIView* view in self.view.subviews) { if ([view isKindOfClass:[/*"which ever class u wa
[self.view setUserInteractionEnabled:NO];
[self.view setUserInteractionEnabled:YES];
如果我这样做,所有it子视图也会受到影响。。。所有视图都已禁用,如何仅针对特定视图执行操作?可能吗
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[/*"which ever class u want eg UITextField "*/ class]])
[view setUserInteractionEnabled:NO];
}
希望能有帮助。快乐编码:)假设您的另一个视图是一个成员,或者您可以遍历
self.view
的子视图数组,这是完全相同的,如下所示:
MyViewController.h
UIView* otherView;
MyViewController.m
otherView.userInteractionEnabled = NO; // or YES, as you desire.
或:
for(int i=0;i<[[self.view子视图]计数];i++)
{
UIView*view=[[self.view子视图]objectAtIndex:i];
//现在,要么检查view的tag属性,要么检查您所知道的其他属性
//它是您想要的,然后更改userInteractionEnabled属性。
}
最好的选择是使用视图的标记
属性,而不是迭代其所有子视图。只需将标记设置为要禁用交互的子视图,并使用下面的代码访问它并禁用交互
// 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];
在swift
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
谢谢,这对我有用,但是我有多个视图,所以我需要将它们全部隐藏起来。我做的另一件事是创建一个带有背景的视图,使我禁用的视图变灰。如果使用这个elf.view.userInteractionEnabled=false,但我只能启用一个按钮,怎么样?self.view.userInteractionEnabled=false将停止应用程序UI以响应任何触摸(根据问题)。如果只想启用一个按钮,则不应使用此代码。您可以通过某种方式禁用单个UIView元素以停止对触摸的响应。哦,我明白了,这就是为什么我问是否有办法禁用除一个之外的所有元素。该属性已重命名为
isUserInteractionEnabled
。
// 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