6

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

4

4 回答 4

13

UIViews有一个名为mask.

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

所以,你的方法应该是这样的:

(这是为 Swift 设计的,但它很容易转换为 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

更新

  • 删除了 Swift 2 引用,因为它不再相关。出于好奇,该属性被称为maskView
于 2016-05-12T22:27:26.250 回答
8

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

于 2011-08-08T18:51:27.500 回答
3

我会去一个白色背景的视图:

whiteView=[[UIView alloc]initWithFrame:viewToDim.frame];
[whiteView setBackgroundColor:[UIColor whiteColor]];
[whiteView setAlpha:0.5f];
[self.view insertSubview:whiteView aboveSubview:viewToDim];
于 2014-06-24T21:04:46.960 回答
0
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()
        }
    }
}
于 2017-01-20T20:17:19.150 回答