如何调暗父UIView(50%透明)登录?

我有一个带登录按钮的视图。 单击该按钮时,我添加了一个包含用于登录的字段的视图。 发生这种情况时,我需要调暗父视图。 我是怎么做到的?

在父视图上添加UIView,该视图最初是透明的,背景颜色为黑色。 当您需要调暗它时,将视图的alpha更改为0.5。 这将是50%透明。

斯威夫特2

maskView有一个名为maskView的属性。

maskView将始终位于拥有它的UIView之上。

所以,你的方法应该是这样的(这适用于Swift,但它很容易转换为Obj-c):

 self.view.maskView = UIView(frame: self.view.frame) self.view.maskView?.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5) //Do your work here, block UI, anything you need to, and then... self.view.maskView = nil 

斯威夫特3

属性名称已更改为mask

 self.view.mask = UIView(frame: self.frame) self.view.mask?.backgroundColor = UIColor.black.withAlphaComponent(0.5) //Do your work here, block UI, anything you need to, and then... self.view.mask = nil 

我会去白色背景的视图:

  whiteView=[[UIView alloc]initWithFrame:viewToDim.frame]; [whiteView setBackgroundColor:[UIColor whiteColor]]; [whiteView setAlpha:0.5f]; [self.view insertSubview:whiteView aboveSubview:viewToDim]; 
 class UIDecorator: NSObject { static let sharedInstance = UIDecorator() private let dimView = UIView() private let loadingView = MOOverWatchLoadingView(frame: CGRectMake(0, 0, 100, 100), autoStartAnimation: true) func showLoadingView() { if let currentPage = UIApplication.topViewController(){ dimView.frame = currentPage.view.frame dimView.backgroundColor = UIColor.blackColor() dimView.alpha = 0.5 currentPage.view.addSubview(dimView) currentPage.view.userInteractionEnabled = false loadingView.center = currentPage.view.center loadingView.backgroundColor = UIColor.clearColor() currentPage.view.addSubview(loadingView) } } func dismissLocadingView() { if let currentPage = UIApplication.topViewController(){ currentPage.view.userInteractionEnabled = true dimView.removeFromSuperview() loadingView.removeFromSuperview() } } }