SwiftUI-如何更改视图的背景颜色? [英] SwiftUI - How do I change the background color of a View?
本文介绍了SwiftUI-如何更改视图的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开始尝试SwiftUI
,但令我惊讶的是,更改View
的背景颜色似乎并不简单.您如何使用SwiftUI
做到这一点?
I'm beginning to try out SwiftUI
and I'm surprised that it doesn't seem to be straightforward to change the background color of a View
. How do you do this using SwiftUI
?
推荐答案
您可以执行以下操作:
.background(Color.black)
围绕您的观点.
例如来自默认模板(我将其包含在一个组中):
eg. from the default template (I am encompassing it around a Group):
var body: some View {
VStack {
Text("Hello SwiftUI!")
}
.background(Color.black)
}
要为其添加一些不透明度,还可以添加.opacity
方法:
To add some opacity to it, you can add the .opacity
method as well:
.background(Color.black.opacity(0.5))
您还可以通过CMD使用视图的检查元素+单击视图,然后单击Show SwiftUI Inspector
> Background
>您的颜色
You can also make use of the inspect element of the view by CMD + click on the View and clicking Show SwiftUI Inspector
> Background
> Your Color
这篇关于SwiftUI-如何更改视图的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文