我有一个视图,里面有一个登录按钮。当点击该按钮时,我会添加一个包含登录字段的视图。在此期间,我需要将父视图变暗。如何实现呢?
我有一个视图,里面有一个登录按钮。当点击该按钮时,我会添加一个包含登录字段的视图。在此期间,我需要将父视图变暗。如何实现呢?
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
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()
}
}
}