开发者

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

开发者 https://www.devze.com 2023-03-26 13:37 出处:网络
I have a view with a Login button. When the button is clicked, I add a view with field开发者_Python百科s for login. When this happens, I need to dim the parent view. How I do that?UIViews have a prope

I have a view with a Login button. When the button is clicked, I add a view with field开发者_Python百科s for login. When this happens, I need to dim the parent view. How I do that?


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];


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()
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消