Avn*_*arr 80 objective-c alpha-transparency uiview ios
根据文件 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];
Run Code Online (Sandbox Code Playgroud)
然后将子视图添加到'myView'
[myView addSubView anotherView];
anotherView.alpha = 1;
NSLog(@"anotherView alpha = %f",anotherView.alpha); // prints 1.0000 as expected
Run Code Online (Sandbox Code Playgroud)
但' anotherView '在屏幕上确实有alpha(它不像预期的那样不透明)
这怎么可能,可以做些什么?
Abh*_*ert 112
我认为这是文档中的错误.你应该在bugreport.apple.com上提交.
经过一些快速研究后我能看到的一切表明你所看到的是它总是表现得如何,我自己的测试也表明了这一点.
视图的alpha应用于所有子视图.
也许你需要的只是[[UIColor blackColor] colorWithAlphaComponent:0.5]
但如果不是你需要让视图成为兄弟而不是孩子.
Ano*_* VM 42
不要直接在父视图上设置alpha.而不是使用下面的代码行,它将透明度应用于父视图而不影响其子视图.
[parentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];
Naz*_*kyi 25
在迅速
view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
Run Code Online (Sandbox Code Playgroud)
更新SWIFT 3
view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
Run Code Online (Sandbox Code Playgroud)
MRi*_*n33 19
设置背景颜色的不透明度而不是alpha不会影响其子视图.
或者您可以通过程序设置
var customView:UIView = UIView()
customView.layer.opacity = 0.3
Run Code Online (Sandbox Code Playgroud)
而已.快乐的编码!!!
Ral*_*adt 15
如果您喜欢Storyboards,User Defined Runtime Attribute
请在以下位置添加您的视图Identity Inspector
:
Key Path: backgroundColor
,Type: Color
,Value:
例如白色与不透明度50%.
所讨论的最简单的解决方案是更改alpha,如下所示:Xcode 8 Swift 3的更新版本是:
yourParentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
Run Code Online (Sandbox Code Playgroud)
目标C:
yourParentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
Run Code Online (Sandbox Code Playgroud)
请参阅Apple Developer Docs:https: //developer.apple.com/reference/uikit/uiview/1622417-alpha
在 Swift 4.2 和 Xcode 10.1 中
不要通过故事板添加颜色和 alpha 值。在这种情况下,只有程序化的方法才有效。
transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Run Code Online (Sandbox Code Playgroud)