在UIView上设置alpha会在其子视图上设置alpha,这是不应该发生的 [英] Setting alpha on UIView sets the alpha on its subviews which should not happen

查看:85
本文介绍了在UIView上设置alpha会在其子视图上设置alpha,这是不应该发生的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据UIVIew @property(nonatomic) CGFloat alpha

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

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. This value affects only the current view and does not affect any of its embedded subviews.

我有一个配置如下的容器视图:

I have a container view configured as follows:

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

但是' anotherView '的确在屏幕上显示了Alpha(并非如预期那样不透明)

But 'anotherView' does have alpha on screen (it is not opaque as expected)

这怎么办?可以做什么?

How can this be and what can be done?

推荐答案

我认为这是文档中的错误.您应该将其提交到bugreport.apple.com.

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.

视图的Alpha应用于所有子视图.

The alpha of a view is applied to all subviews.

也许您只需要[[UIColor blackColor] colorWithAlphaComponent:0.5],但是如果没有,您将需要使视图成为同级视图而不是子视图.

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.

这篇关于在UIView上设置alpha会在其子视图上设置alpha,这是不应该发生的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆