ios 在另一个具有非透明内容的透明 UIView 上的透明视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8882402/
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
Transparent view on another transparent UIView with non-transparent content?
提问by Krumelur
The idea is to show a progress dialog. I'm trying to cover a parent view (normally the whole screen) with a transparent black view (alpha 0.2). On top of that I want to show a smaller view, also transparent with another color. It may be not influenced by color or transparency of the first view. In that 2nd view I want UI elements that are opaque. I have tried everything and even found posts here that deal with something similar but only uses one transparent layer. And those already use weird tricks. Is there a standard, easy way of achieving this?
这个想法是显示一个进度对话框。我试图用透明的黑色视图(alpha 0.2)覆盖父视图(通常是整个屏幕)。最重要的是,我想显示一个较小的视图,也用另一种颜色透明。它可能不受第一个视图的颜色或透明度的影响。在第二个视图中,我想要不透明的 UI 元素。我已经尝试了所有方法,甚至在这里找到了处理类似内容但只使用一个透明层的帖子。而那些已经使用了奇怪的技巧。有没有标准的,简单的方法来实现这一目标?
采纳答案by Krumelur
I found an answer in a blog: http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
我在博客中找到了答案:http: //cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
The solution is to override drawRect:
and take care of the alpha in it. You may not touch UIView's
alpha property, nor may you set the view's background color to anything but transparent. All drawing must be made in drawRect:.
This way I was able to stack transparent views and put opaque elements on top.
解决方案是覆盖drawRect:
并处理其中的 alpha。您不能触摸UIView's
alpha 属性,也不能将视图的背景颜色设置为透明以外的任何颜色。所有绘图都必须以drawRect:.
这种方式进行,我能够堆叠透明视图并将不透明元素放在顶部。
回答by fzwo
Without any hacking around or complicated and possibly slow custom drawRect:
, this is possible if you group your views:
没有任何黑客攻击或复杂且可能缓慢的 custom drawRect:
,如果您对视图进行分组,这是可能的:
Create a bounding view that encompasses and holds the whole dialog. This view itself has no visible content, and its backgroundColor is clear. Its alpha is 1.0.
创建一个包含并保持整个对话框的边界视图。这个视图本身没有可见的内容,它的背景颜色是清晰的。它的 alpha 是 1.0。
Now add all the transparent views (those with alpha < 1) to this that you want, and also add the non-transparent views. Be careful not to add any non-transparent views as subviews of the transparent ones, instead add them as direct subviews of the bounding view. That way, they will inherit its alpha of 1.0.
现在将所有透明视图(alpha < 1 的那些)添加到您想要的,并添加非透明视图。注意不要将任何非透明视图添加为透明视图的子视图,而是将它们添加为边界视图的直接子视图。这样,他们将继承其 alpha 1.0。
UIView *progressDialogView = [[UIView alloc] initWithFrame:dialogFrame];
progressDialogView.backgroundColor = [UIColor clearColor];
progressDialogView.alpha = 1.0; //you can leave this line out, since this is the default.
UIView *halfTransparentBackgroundView = [[UIView alloc] initWithFrame:dialogFrame];
halfTransparentBackgroundView.backgroundColor = [UIColor blackColor]; //or whatever...
halfTransparentBackgroundView.alpha = 0.5;
[progressDialogView addSubview: halfTransparentBackgroundView];
UIView *progressBarView = [[UIView alloc] initWithFrame:progressBarFrame];
progressBarView.backgroundColor = [UIColor blueColor];
[progressDialogView addSubview: progressBarView];
回答by Bob Fenchel
Instead of setting the alpha property, use the following for setting the background color / opacity for each view:
不要设置 alpha 属性,而是使用以下内容为每个视图设置背景颜色/不透明度:
view1.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
Use whatever values you desire for red, green, blue, and alpha.
使用任何您想要的红色、绿色、蓝色和 alpha 值。
回答by Flea
I needed to have transparent view on top of another view. The view to make transparent I just did:
我需要在另一个视图之上有透明视图。使透明的视图我刚刚做了:
self.alpha = 0.5;
For example:
例如:
- (void) configureView
{
self.frame = CGRectMake(0, 0, 640, 960);
self.backgroundColor = [UIColor greyColor];
self.alpha = 0.5;
}
回答by Patrick Hernandez
Adding your opaque view as a subview of the transparent view won't work. What I have done in the past is have a UIView
class that adds a semi-transparent subview and an opaque subview. That way the transparent view won't affect the opaque one.
将不透明视图添加为透明视图的子视图是行不通的。我过去所做的是有一个UIView
添加半透明子视图和不透明子视图的类。这样透明视图不会影响不透明视图。