以编程方式 Swift UIView 不透明度 [英] Swift UIView Opacity Programmatically

查看:41
本文介绍了以编程方式 Swift UIView 不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用 UIView 的属性来以编程方式实现所需的不透明外观,而这可以通过 Storyboard 和 Attributes Inspector 轻松完成.

I'm having quite a hard time messing with UIView's properties to achieve a desired opaque look programmatically that can be easily done through Storyboard and Attributes Inspector.

我想重新创建的内容(来自属性检查器的设置):背景色为 (RGB Sliders: 0, 0, 0) 且不透明度滑块设置为 75%,alpha 默认为 1 的 UIView.所以基本上是一个黑色的 UIView,不透明度调低.

What I want to re-create (settings from attributes inspector): A UIView with a background color of (RGB Sliders: 0, 0, 0) and Opacity Slider set at 75%, with alpha defaulted at 1. So basically a black UIView with the opacity toned down.

我以编程方式尝试过的:

What I've tried programmatically:

1)  view.backgroundColor = .black
    view.alpha = 0.75

2)  view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
    view.isOpaque = true

附上一张在 Storyboard 中选择的 UIView 的图片以及设置供您查看.如果您需要更多信息,请随时告诉我.非常感谢您的帮助.

Attached is a picture of the UIView selected in Storyboard with the settings for you to see. If you need any more information, please don't hesitate to let me know. Much thanks for your help.

更新:感谢您的所有投入.Clever Error 对视图层的解释和 Matt 的代码相结合,使我能够实现我想要的外观.

UPDATE: Thanks for all your input. The combination of Clever Error's explanation of view layers and Matt's code allowed for me to achieve my desired look.

想要的 UI 外观!

推荐答案

您所描述的是:

view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
view.isOpaque = false

视图不能被标记为不透明,因为它不是不透明的.

The view must not be marked opaque, as it is not opaque.

这篇关于以编程方式 Swift UIView 不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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