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

查看:24
本文介绍了在 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天全站免登陆