iOS: Is there a way to make a viewcontroller look inactive by having everything grayed out?

本秂侑毒 提交于 2019-12-14 01:23:28

问题


I want my ViewController and every object appear inactive by having it all grayed out (kind of like how the UIAlertView popup grays out everything in the background). I do not want to have to manually gray out every object there is.Is there an easy way to make this work?


回答1:


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

You can mess with the white and alpha values to get exactly what you want, but that should put a gray tint over everything in your view controller's view.




回答2:


As Patrick Goley pointed with his good answer - just put a gray UIView with alpha between 0.5 - 0.75 above your original view. But this method will disable all the user interactions with its subviews (buttons,etc). If you want to preserve user interactions with the elements but nevertheless mark the view as "inactive", you should subclass the view which you will put above your original view (which you want to mark as inactive) and in this subclass override this method:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {

// Original "inactive" UIView will now respond to touch events if return NO:

  return NO;
 }



回答3:


This code will cover also any parent controller: UINavigationController | UITabBarController.
Don't forget #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];



回答4:


I prefer to add a UIView that has a black background, and then lower the alpha.

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

And then do not forget to add the subview to your main view

[self.view addSubView:grayView];



回答5:


Yes:

Set the alpha of the view

[self.view setAlpha:0.5];


来源:https://stackoverflow.com/questions/19710749/ios-is-there-a-way-to-make-a-viewcontroller-look-inactive-by-having-everything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!