在 UIView 上设置 alpha 将在其子视图上设置 alpha,这是不应该发生的

根据 UIVIew @property(nonatomic) CGFloat alpha的文件

此属性的值是范围为0.0的浮点数 到1.0,其中0.0代表完全透明,1.0代表 完全不透明。 此值仅影响当前视图,不影响其任何嵌入的子视图。

我的容器视图配置如下:

self.myView.backgroundColor = [UIColor blackColor];
self.myView.alpha = 0.5;
[self addSubview:self.myView];

然后将子视图添加到 “ myView”

[myView addSubView anotherView];
anotherView.alpha = 1;
NSLog(@"anotherView alpha = %f",anotherView.alpha); // prints 1.0000 as expected

但是“ 另一个视角”在屏幕上确实有 alpha (它并不像预期的那样不透明)

这怎么可能,我们能做些什么?

52330 次浏览

I think this is a bug in the documentation. You should file it at bugreport.apple.com.

Everything I can see after a bit of quick research suggests what you are seeing is how it always has behaved, and my own testing shows it too.

The alpha of a view is applied to all subviews.

Perhaps all you need is [[UIColor blackColor] colorWithAlphaComponent:0.5] but if not you will need to make the view a sibling instead of a child.

Don't set the alpha directly on the parent view. Instead of it use the below line of code which will apply transparency to parentview without affecting its child views.

[parentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];

If you like Storyboards, put a User Defined Runtime Attribute for your view in the Identity Inspector:

Key Path: backgroundColor, Type: Color, Value: e.g. white color with Opacity 50 %.

Here is a bit complex solution:

UIView *container;
UIView *myView;
UIView *anotherView;


myView.alpha = 0.5;
[container addSubview:myView];


anotherView.alpha = 1;
[container addSubview:anotherView];

Use a container view as superview, anotherView and myView are both subview in container, anotherView is not a subview in myView.

In swift

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

UPDATED FOR SWIFT 3

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

Simplest solution as discussed is to change the alpha as follows : Updated version for Xcode 8 Swift 3 is :

yourParentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)

Objective C:

yourParentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

Refer Apple Developer Docs here : https://developer.apple.com/reference/uikit/uiview/1622417-alpha

Set Opacity of the background color instead of alpha will not affect its child views.

  1. select view.
  2. go to attribute inspector than background color
  3. click on "others"
  4. set opacity to 30%

Or you can set by programmetically

var customView:UIView = UIView()
customView.layer.opacity = 0.3

Thats it. Happy Coding!!!

For now there is only one way make the Parent View transparent and don't put any child views inside (don't put any views as subview) the parent view, put that child views outside of the parent view. To make parent view transparent you can do this via storyboard.

//Transparent the parentView


parentView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)

Put the other view outside of the parent view. It will work like a charm.

In Swift 4.2 and Xcode 10.1

Don't add colour and alpha value through storyboard. Only programmatic approach will work in this case.

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

Please refer to the bold description from Xcode documentation.

The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. Changing the value of this property updates the alpha value of the current view only. However, the transparency imparted by that alpha value affects all of the view's contents, including its subviews. For example, a subview with an alpha value of 1.0 that is embedded in a parent view with an alpha value of 0.5, appears onscreen as if its alpha value is also 0.5.