objective-c 100% 不透明度 UILabel 超过 50% 不透明度背景(UIView?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1885198/
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
100% opacity UILabel over a 50% opacity background (UIView?)
提问by DevDevDev
So right now I have a UIViewwith a UILabelin it. I want the background to have an opacity < 1.0 and the label to have an opacity of 1.0. However since alphas propagate down the view hierarchy, the label ends up with an opacity < 1.0 as well.
所以现在我有一个UIView与UILabel它。我希望背景的不透明度 < 1.0,标签的不透明度为 1.0。但是,由于 alpha 沿着视图层次结构向下传播,因此标签最终的不透明度也小于 1.0。
Is there anyway to do what I want without making the UILabela subview of another view??
无论如何,在不制作UILabel另一个视图的子视图的情况下做我想做的事吗?
回答by Ian Henry
Just set the background color to be semitransparent:
只需将背景颜色设置为半透明:
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
Or, in Swift:
或者,在 Swift 中:
view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
Or, Swift 3:
或者,斯威夫特 3:
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Note that, in this particular case, UIColor(white: 0, alpha: 0.5)is more concise, but colorWithAlphaComponentwill work in general.
请注意,在这种特殊情况下,UIColor(white: 0, alpha: 0.5)它更简洁,但colorWithAlphaComponent通常会起作用。
回答by Ja?ck
Besides being available in code, you can do this quite easily from iB as well:
除了在代码中可用之外,您还可以从 iB 轻松完成此操作:
- Within the storyboard, select the view you wish to edit;
- From the right panel, make sure the Attributes inspector is opened;
- Click on the right side of the "Background" drop down box and choose "Other ..."; it will open a colour picker dialog;
- Change the "Opacity" at the bottom to set the background colour opacity.
- 在故事板中,选择要编辑的视图;
- 在右侧面板中,确保属性检查器已打开;
- 点击右侧的“背景”下拉框,选择“其他...”;它将打开一个颜色选择器对话框;
- 更改底部的“不透明度”以设置背景颜色不透明度。
回答by Colin Gislason
You can set the background color of the UIViewwith a semi-transparent color or make the image itself semi-transparent. This way it's a property of the view that is transparent, not the view itself.
您可以UIView使用半透明颜色设置背景颜色或使图像本身半透明。这样,它是视图的透明属性,而不是视图本身。
回答by amedc111
You can use this:
你可以使用这个:
self.view.layer.opacity=0.5

