如何在当前视图上方创建半透明视图层?

use*_*ser 18 objective-c uikit ios

您之前可能已经看过它,它在像ScoutMob这样的消费时尚应用中变得非常流行.我正在尝试在启动时实现60%透明视图,这将覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失.

我完成了我的整个应用程序(它是从几年前开始使用.xib的,但是也可以用故事板格式来解释,因为我可能会在其他iOs 5.0+应用程序中重用此功能.)

我制作单个视图没有问题,但是暂时将一个视图叠加在另一个上面是我没有直观地想出来的.我将继续研究并包括我发现的任何有用的提示,以防其他人试图做同样的事情.

Rif*_*nio 47

// get your window screen size
CGRect screenRect = [[UIScreen mainScreen] bounds];
//create a new view with the same size
UIView* coverView = [[UIView alloc] initWithFrame:screenRect];
// change the background color to black and the opacity to 0.6
coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
// add this new view to your main view
[self.view addSubview:coverView];
Run Code Online (Sandbox Code Playgroud)

当你完成它,你可以删除它:

[coverView removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)

Swift3版本:

// get your window screen size
let screenRect = UIScreen.main.bounds
//create a new view with the same size
let coverView = UIView(frame: screenRect)
// change the background color to black and the opacity to 0.6
coverView.backgroundColor = UIColor.black.withAlphaComponent(0.6)        
// add this new view to your main view
self.view.addSubview(coverView)
Run Code Online (Sandbox Code Playgroud)

删除它:

coverView.removeFromSuperview()
Run Code Online (Sandbox Code Playgroud)