[英]SwiftUI: background color for whole screen and not simple view?
I am trying to do something very simple, display a view with a background color, and in the center of this view, display a single label.我正在尝试做一些非常简单的事情,显示一个带有背景颜色的视图,并在此视图的中心显示一个标签。
I tried this:我试过这个:
var body: some View {
VStack {
Text("Hello!")
}
.background(MyColors.blue)
.ignoresSafeArea()
}
Here is the given result:这是给定的结果:
What am I doing wrong?我究竟做错了什么?
Thank you for your help感谢您的帮助
You can do it like this:你可以这样做:
Text("Hello!")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(MyColors.blue)
.ignoresSafeArea()
Source: https://www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them资料来源: https : //www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them
I managed to get it working with this code:我设法让它与此代码一起工作:
var body: some View {
ZStack {
Style.Color.red
VStack {
Text("Hello!")
}
}
.ignoresSafeArea()
}
I didn't understand first that background and rest of the view must be set on a different z-axis.我首先不明白背景和视图的其余部分必须设置在不同的 z 轴上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.