[英]alpha for background color not working in iOS
If you are using storyboard and present view controller using segue, the presentation attribute of segue that show you view controller with transparent background should be over full screen as shown in attachment.如果您使用情节提要并使用 segue 呈现视图控制器,则显示具有透明背景的视图控制器的 segue 的呈现属性应全屏显示,如附件所示。
Then in your view controller set the background color by using this :然后在您的视图控制器中使用此设置背景颜色:
self.view.backgroundColor = UIColor(white: 0, alpha: 0.4)
It's not strange, it's behaving exactly as it should.这并不奇怪,它的行为完全符合它应有的要求。 Although many of UIColor's methods are class methods, there are still a few instance methods, and this is one of them. UIColor的方法虽然很多都是类方法,但还是有一些实例方法,这就是其中之一。 From the UIColor documentation .来自 UIColor 文档。
colorWithAlphaComponent: colorWithAlphaComponent:
Creates and returns a color object that has the same color space and component values as the receiver, but has the specified alpha component.创建并返回一个颜色对象,该对象具有与接收器相同的颜色空间和分量值,但具有指定的 alpha 分量。 So, colorWithAlphaComponent: just changes the alpha value of its receiver.所以, colorWithAlphaComponent: 只是改变其接收器的 alpha 值。 Example:例子:
let black = UIColor.blackColor() // 1.0 alpha
self.view.backgroundColor = black.colorWithAlphaComponent(0.5) // 0.5 alpha
您正在呈现ViewController,因此它将无法工作,因为您必须在其他 ViewController 上添加UIViewController 的UIView ,然后才能工作。
[[self view] addSubview:myViewController.view];
I found an issue on iOS 12 which is related to the title (but not the answers though) where withAlphaComponent does not work.我在 iOS 12 上发现了一个与标题(但不是答案)相关的问题,其中 withAlphaComponent 不起作用。
Expected result: the view background colour is the the named one with some alpha.预期结果:视图背景颜色是带有一些 alpha 的命名颜色。 Actual result: the view background colour is he named colour without alpha.实际结果:视图背景颜色是他命名的颜色,没有 alpha。
The issue does not occur if the colour set in the storyboard is any colour but the named one.如果情节提要中设置的颜色是命名颜色以外的任何颜色,则不会出现此问题。 It does not occur on iOS 14.1 either.它也不会发生在 iOS 14.1 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.