iOS:有没有办法使一个viewcontroller看起来不活动一切变灰?

我希望我的ViewController和每个对象都显示为非活动状态(类似于UIAlertViewpopup窗口如何灰色背景中的所有内容)。 我不想手动灰化每个对象。是否有一个简单的方法来做到这一点?

UIView *grayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; grayView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5]; [self.view addSubview:grayView]; 

你可以混淆白色和alpha值来得到你想要的东西,但是应该在视图控制器视图中的所有东西上留下灰色的色调。

正如帕特里克·戈利(Patrick Goley)指出的那样,他的答案很好 – 只需要把一个灰色的UIView放在你的原始视图之上0.5到0.75之间。 但是,这种方法将禁用所有的用户与其子视图(button等)的交互。 如果你想保持与元素的用户交互,但是仍然把视图标记为“非活动”,那么你应该把你放在原始视图之上的视图(你想标记为不活动)inheritance,并在这个子类中覆盖这个方法:

 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { // Original "inactive" UIView will now respond to touch events if return NO: return NO; } 

这段代码也将覆盖任何父控制器:UINavigationController | 的UITabBarController。
不要忘记#import“AppDelegate.h”。

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; UIView *grayView = [[[UIView alloc] initWithFrame:appDelegate.window.bounds] autorelease]; grayView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.8]; [appDelegate.window addSubview:grayView]; 

我更喜欢添加具有黑色背景的UIView,然后降低Alpha。

 UIView *grayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; grayView.backgroundColor = [UIColor blackColor]; [grayView setAlpha:0.65]; 

然后不要忘记添加子视图到你的主视图

 [self.view addSubView:grayView]; 

是:

设置视图的alpha

 [self.view setAlpha:0.5];