背景色彩不透明度

我有一个 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一样,你可以在一个 UICollectionView例如。

有人知道怎么解决这个问题吗?

剪辑 因为几个答案大致相同,我在这里输入。 现在我甚至试过这些:

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

还有

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

标签也会受到 alpha 的影响,它的不透明度是50% 。我不知道我做错了什么,因为我只把颜色 alpha 设置为0.5,而没有设置标签。有什么想法吗?

221398 次浏览

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

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

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

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

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)

or

view.backgroundColor = UIColor.red.withAlphaComponent(0.5)

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

view.backgroundColor = UIColor.white.withAlphaComponent(0.5)

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

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

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

enter image description here

For Swift 4.x and above

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

The question is old, but it seems that there are people who have the same concerns.

What do you think of the opinion that 'the alpha property of UIColor and the opacity property of Interface Builder are applied differently in code'?


The two views created in Interface Builder were initially different colors, but had to be the same color when the conditions changed. So, I had to set the background color of one view in code, and set a different value to make the background color of both views the same.

As an actual example, the background color of Interface Builder was 0x121212 and the Opacity value was 80%(in Amani Elsaed's image :: Red: 18, Green: 18, Blue: 18, Hex Color #: [121212], Opacity: 80), In the code, I set the other view a background color of 0x121212 with an alpha value of 0.8.

self.myFuncView.backgroundColor = UIColor(red: 18, green: 18, blue: 18, alpha: 0.8)

extension is

extension UIColor {
convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0) {
self.init(red: CGFloat(red) / 255.0,
green: CGFloat(green) / 255.0,
blue: CGFloat(blue) / 255.0,
alpha: alpha)
}
}

However, the actual view was

  • 'View with background color specified in Interface Builder': R 0.09 G 0.09 B 0.09 alpha 0.8.
  • 'View with background color by code': R 0.07 G 0.07 B 0.07 alpha 0.8

Calculating it,

  • 0x12 = 18(decimal)
  • 18/255 = 0.07058...
  • 255 * 0.09 = 22.95
  • 23(decimal) = 0x17

So, I was able to match the colors similarly by setting the UIColor values ​​to 17, 17, 17 and alpha 0.8.

self.myFuncView.backgroundColor = UIColor(red: 17, green: 17, blue: 17, alpha: 0.8)

Or can anyone tell me what I'm missing?