ios Swift UIView 背景颜色不透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27439220/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:00:04  来源:igfitidea点击:

Swift UIView background color opacity

iosswiftuiviewalpha

提问by user2099024

I have a UIViewwith a UILabelin it. I want the UIView to have white background color, but with an opacity of 50%. The problem whith setting view.alpha = 0.5is that the label will have an opacity of 50% as well, so I figured out that it maybe would be possible to have a UIViewwith white background color and opacity (white_view), and then have another UIViewwith the label (label_view). Then add the "white_view" to "label_view" by doing this: label_view.addSubview(white_view). This apparently doesn't work. I'd like to do like: label_view.backgroundView(white_view)but you can't set a background view on a UIViewlike you can do in a UICollectionViewfor instance.

我有UIView一个UILabel在里面。我希望 UIView 具有白色背景色,但不透明度为 50%。设置的问题view.alpha = 0.5是标签的不透明度也为 50%,所以我发现可能有一个UIView白色背景颜色和不透明度(white_view),然后有另一个UIView标签(label_view) )。然后,通过这样添加“white_view”到“label_view”: label_view.addSubview(white_view)。这显然行不通。我想这样做:label_view.backgroundView(white_view)但是你不能UIView像在 a 中那样设置背景视图UICollectionView

Does anyone have any clue of how to solve this?

有没有人知道如何解决这个问题?

EDITBecause several answers are approx the same I'll type it here. Now I've tried even these:

编辑因为几个答案大致相同,所以我会在这里输入。现在我什至尝试过这些:

label_view1.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
label_view1.addSubview(firstPlacelbl)
endGameView.addSubview(label_view1)

and

label_view1.backgroundColor = UIColor(white: 1, alpha: 0.5)
label_view1.addSubview(firstPlacelbl)
endGameView.addSubview(label_view1)

And still the label is also affected by the alpha, and it gets an opacity of 50%. I don't get it what I do wrong because I only set the colors alpha to 0.5 and not the labels. Any ideas?

而且标签也受 alpha 的影响,它的不透明度为 50%。我不明白我做错了什么,因为我只将颜色 alpha 设置为 0.5 而不是标签。有任何想法吗?

回答by Vitaliy Gozhenko

You can set background color of view to the UIColor with alpha, and not affect view.alpha view?.backgroundColor = UIColor(white: 1, alpha: 0.5)

您可以使用 alpha 将视图的背景颜色设置为 UIColor,而不会影响 view.alpha view?.backgroundColor = UIColor(white: 1, alpha: 0.5)

回答by mustafa

Setting alphaproperty of a view affects its subviews. If you want just transparent background set view's backgroundColorproprty to a color that has alpha component smaller than 1.

设置alpha视图的属性会影响其子视图。如果您只想透明背景将视图的属性设置backgroundColor为具有小于 1 的 alpha 分量的颜色。

view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)

回答by Ashu

in Swift 3.0

在 Swift 3.0 中

yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)

yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)

This works for me in xcode 8.2.

这在 xcode 8.2 中对我有用。

It may helps you.

它可能会帮助你。

回答by Amani Elsaed

You can also set it from InterfaceBuilderby changing color's opacity:

您还可以InterfaceBuilder通过更改颜色的不透明度来设置它:

enter image description here

在此处输入图片说明

回答by Marcus

The problem you have found is that viewis different from your UIView. 'view' refers to the entire view. For example your home screen is a view.

您发现的问题view与您的UIView. “视图”是指整个视图。例如,您的主屏幕是一个视图。

You need to clearly separate the entire 'view' your 'UIView' and your 'UILabel'

您需要将整个“视图”清楚地分开,您的“UIView”和“UILabel”

You can accomplish this by going to your storyboard, clicking on the item, Identity Inspector, and changing the Restoration ID.

您可以通过转到故事板,单击项目、身份检查器并更改Restoration ID.

Now to access each item in your code using the restoration ID

现在使用恢复 ID 访问代码中的每个项目

回答by Abhimanyu Rathore

It's Simple in Swift . just put this color in your background view color and it will work .

在 Swift 中很简单。只需将此颜色放在您的背景视图颜色中,它就会起作用。

let dimAlphaRedColor = UIColor.redColor().colorWithAlphaComponent(0.7) yourView.backGroundColor = dimAlphaRedColor

let dimAlphaRedColor = UIColor.redColor().colorWithAlphaComponent(0.7) yourView.backGroundColor = dimAlphaRedColor