如何使用 SwiftUI 创建带有 alpha 值的颜色? [英] How to create a color with an alpha value using SwiftUI?
本文介绍了如何使用 SwiftUI 创建带有 alpha 值的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 UIKit
中创建带有 alpha 值的颜色有不同的方法,例如:
In UIKit
to create a color with an alpha value there are different ways, for example:
UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
但是这个方法在SwiftUI
的对应类Color
上没有.
but this method is not available on the corresponding class Color
of SwiftUI
.
我可以使用什么?
推荐答案
在 SwiftUI
中有一个特定的 modifier 叫做 opacity
可以用于更改任何颜色的 alpha 级别
In SwiftUI
there is one specific modifier called opacity
that can be use to change the alpha level of any colors
Text("Hello world").background(Color.black.opacity(0.8)) // Set the background color as partially transparent.
此外,相同的修饰符可以应用于任何视图,以更改其 alpha.
Additionally the same modifier can be applied to any view, to change its alpha.
Image("myImage").opacity(0.8) //The image will be partially transparent
这篇关于如何使用 SwiftUI 创建带有 alpha 值的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文