How to dim a parent UIView (50% transparent) for login?

UIViews have a property named mask.

mask will always be on top of the UIView who owns it.

So, your approach should be something like this:

(This is for Swift, but it's easily converted to Obj-c)

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

Update

  • Removed Swift 2 refernce as it's not relevant anymore. Just as a curiosity, then the property was called maskView

Add a UIView over the parent view that is initially transparent with a background color of black. When you need to dim it, change the view's alpha to 0.5. This will be 50% transparent.


I would go for a view with white background:

whiteView=[[UIView alloc]initWithFrame:viewToDim.frame];
[whiteView setBackgroundColor:[UIColor whiteColor]];
[whiteView setAlpha:0.5f];
[self.view insertSubview:whiteView aboveSubview:viewToDim];