[英]SwiftUI ZStack first element is not displayed
我是 SwiftUI 的新手,我有一个带有 ZStack 的简单应用程序:
struct ContentView: View {
@State var num : Int = 1
var body: some View {
NavigationView{
ZStack{
Text("asd")
.foregroundColor(.blue)
.frame(width: 400, height: 400, alignment: .center)
.background(.blue)
VStack{
List{
ListItem()
ListItem()
}
}
.toolbar{
Button{
num+=1
} label: {
Label("Add", systemImage: "plus")
}
}
}
}
}
}
为什么会这样?
您正在使用 ZStack 来总结所有内容。
解决方案:从 ZStack 更改为 VStack。
NavigationView{
VStack{ //this part
这是因为Text View
在ZStack
中的List
下面。
如果将文本移动到列表之后,它将显示在顶部。
ZStack {
VStack{
List {
ListItem()
ListItem()
}
.listStyle(.insetGrouped)
}
.toolbar{
Button{
num+=1
} label: {
Label("Add", systemImage: "plus")
}
}
Text("asd")
.foregroundColor(.blue)
.frame(width: 400, height: 400, alignment: .center)
.background(.blue)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.