ios 如何在当前视图上方创建半透明视图层?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16283324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a semi-transparent view layer above a current view?
提问by user
You've likely seen this before, its become exceedingly popular in consumery chic apps like ScoutMob. I'm trying to implement a 60% transparent view on launch that will cover my home screen, explaining how to use the functions of the home screen and disappears on tap.
您之前可能已经看到了这一点,它在 ScoutMob 等消费时尚应用程序中变得非常流行。我正在尝试在启动时实现 60% 的透明视图,该视图将覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失。
I have my entire app completed (it's using .xib's since its from a years ago, but feel free to explain in storyboard format as well, since I will likely reuse this feature in other iOs 5.0+ applications.)
我已经完成了我的整个应用程序(它从几年前开始使用 .xib,但也可以随意以故事板格式进行解释,因为我可能会在其他 iOs 5.0+ 应用程序中重用此功能。)
I have no problem making single views, but temporarily layering one on top of another is something I haven't figured out intuitively. I'll continue researching and include any helpful tips I find in case other people are trying to do the same thing.
我制作单个视图没有问题,但是暂时将一个视图叠加在另一个视图上是我没有直观地想出的事情。我将继续研究并提供我发现的任何有用提示,以防其他人尝试做同样的事情。
回答by Rifinio
// 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];
when you are done with it , you can remove it :
完成后,您可以将其删除:
[coverView removeFromSuperview];
Swift3 version:
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)
to remove it:
删除它:
coverView.removeFromSuperview()
回答by David
This can be easily accomplished with a UITextView and a UIButton. Simply place the UITextView on the screen with the content you want to display, making it the full frame size of the screen, and change the background color to a black background with background alpha of .6
这可以通过 UITextView 和 UIButton 轻松完成。只需将 UITextView 与要显示的内容一起放置在屏幕上,使其成为屏幕的全帧大小,并将背景颜色更改为背景 alpha 为 0.6 的黑色背景
[UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6];
Then place the button as a subview on top, making it the full frame of the screen, and setting it's alpha to 0. Set the action of the button to hide the textview and button.
然后将按钮作为子视图放在顶部,使其成为屏幕的全帧,并将其alpha设置为0。设置按钮的动作以隐藏文本视图和按钮。
Example:
例子:
UITextView* textview = [[UITextView alloc] initWithFrame:self.view.frame];
[textview setText: @"Text here"];
[textview setBackgroundColor: [UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6]];
[textview setTextColor: [UIColor whiteColor]];
[self.view addSubview: textview];
UIButton* btn = [[UIButton alloc] initWithFrame:self.view.frame];
[btn setAlpha:0];
[btn addTarget:self action:@selector(method) forEvent:UIControlEventTouchUpInside];
[self.view addSubview: btn];
You may want to check the addTarget: method; I'm not sure those are the correct parameters off the top of my head.
您可能需要检查 addTarget: 方法;我不确定这些参数是否正确。
回答by Undo
Just make a mechanism that figures out if it is the first launch of the app, then tells your main view controller to add a (probably image) view on top of the current view with 0.6 alpha
只需创建一个机制来确定它是否是应用程序的第一次启动,然后告诉您的主视图控制器在当前视图的顶部添加一个(可能是图像)视图并使用 0.6 alpha
if (figureOutIfIsFirstLaunch)
{
UIImageView *myOverlay = [...];
myOverlay.frame = self.view.bounds;
myOverlay.alpha = 0.6;
[self.view addSubview:myOverlay];
}
回答by Mohamad Chami
- create
UIView
, name it what you want (dimBackgroundUIView
) then set the backgroundcolor as Black and set alpha to 0.1 or 0.2 or.... - add these 2 lines of code when you need to dim the
background:
[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
- and add these 2 lines of code when you want to remove the dimming
background:
if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];
- do not forgot to add the constraints(layouts) for
dimBackgroundUIView
.
- 创建
UIView
,将其命名为您想要的 (dimBackgroundUIView
),然后将背景色设置为黑色并将 alpha 设置为 0.1 或 0.2 或.... - 当您需要调暗背景时,添加以下两行代码:
[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
- 并在要删除调光背景时添加以下两行代码:
if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];
- 不要忘记为 . 添加约束(布局)
dimBackgroundUIView
。
check this:iOS Dim background when a view is shown
检查这个:iOS Dim background when a view is显示
and do not forget to read the note below the code to understand what you have to do.
并且不要忘记阅读代码下方的注释以了解您必须执行的操作。