簡體   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