繁体   English   中英

SwiftUI ZStack 第一个元素不显示

[英]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 ViewZStack中的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM