ios 在 UIView 上设置 alpha 会在其子视图上设置不应该发生的 alpha

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

Setting alpha on UIView sets the alpha on its subviews which should not happen

iosobjective-cuiviewalpha-transparency

提问by Avner Barr

According to the documentation for UIVIew @property(nonatomic) CGFloat alpha

根据文档 UIVIew @property(nonatomic) CGFloat alpha

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.

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

I have a container view configured as follows:

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

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

And then add subviews to 'myView'

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

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

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

但是“ anotherView”在屏幕上确实有 alpha (它不像预期的那样不透明)

How can this be and what can be done?

这怎么可能,可以做什么?

回答by Abhi Beckert

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

我认为这是文档中的错误。你应该在 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.

视图的 alpha 应用于所有子视图。

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.

也许您所需要的只是[[UIColor blackColor] colorWithAlphaComponent:0.5]但如果不是,您将需要使视图成为兄弟姐妹而不是孩子。

回答by Anooj VM

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.

不要直接在父视图上设置 alpha。而不是使用下面的代码行,它将透明度应用于父视图而不影响其子视图。

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

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

回答by Nazarii Stadnytskyi

In swift

在迅速

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

UPDATED FOR SWIFT 3

为 Swift 3 更新

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

回答by MRizwan33

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

设置背景颜色的不透明度而不是 alpha 不会影响其子视图。

  1. select view.
  2. go to attribute inspector than background color
  3. click on "others"
  4. set opacity to 30%
  1. 选择视图。
  2. 转到属性检查器而不是背景颜色
  3. 点击“其他”
  4. 将不透明度设置为 30%

Or you can set by programmetically

或者您可以通过编程方式设置

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

Thats it. Happy Coding!!!

就是这样。编码快乐!!!

回答by Ralf Hundewadt

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

如果您喜欢故事板,请将User Defined Runtime Attribute您的观点放在Identity Inspector

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

Key Path: backgroundColor, Type: Color,Value:例如白色,不透明度为 50%。

回答by Ankit Kumar Gupta

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

所讨论的最简单的解决方案是按如下方式更改 alpha:Xcode 8 Swift 3 的更新版本是:

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

Objective C:

目标 C:

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

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

请在此处参考 Apple 开发人员文档:https: //developer.apple.com/reference/uikit/uiview/1622417-alpha

回答by iOS

In Swift 4.2 and Xcode 10.1

在 Swift 4.2 和 Xcode 10.1 中

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

不要通过故事板添加颜色和 alpha 值。在这种情况下,只有程序化的方法才有效。

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

回答by Purnendu roy

For now there is only one way make the Parent Viewtransparent 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.

将另一个视图放在父视图之外。它会像魅力一样发挥作用。

回答by likid1412

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 containerview as superview, anotherViewand myVieware both subview in container, anotherViewis not a subview in myView.

使用container视图作为父视图,anotherView并且myView都是 中的子视图containeranotherView不是 中的子视图myView

回答by Danyun Liu

Please refer to the bold description from Xcode documentation.

请参阅 Xcode 文档中的粗体描述。

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.

该属性的值是一个介于 0.0 到 1.0 之间的浮点数,其中 0.0 表示完全透明,1.0 表示完全不透明。更改此属性的值只会更新当前视图的 alpha 值。但是,该 alpha 值赋予的透明度会影响视图的所有内容,包括其子视图。例如,一个 alpha 值为 1.0 的子视图被嵌入到一个 alpha 值为 0.5 的父视图中,在屏幕上显示,就好像它的 alpha 值也是 0.5。