提问者:小点点

无法在SwiftUI中查看全屏


我试图在SwiftUI中构建一个非常简单的应用程序。 我想要的第一件事是我的视图是全屏的,边到边。 在某个地方,应用程序添加了一些填充,但我搞不清楚。

下面是我的代码:

struct ContentView: View {

@ObservedObject var fetcher = ArticleService()

var body: some View {
    List {
        ForEach(fetcher.articles) { article in
            VStack (alignment: .leading) {
                Text(article.section)
                    .foregroundColor(.red)
                    .textCase(.uppercase)
                Text(article.title)
                    .font(.system(size: 20))
                    .fontWeight(.semibold)
                UrlImageView(article.image)
                    .padding(EdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0))
                    .frame(maxWidth: .infinity)
                Text(article.author)
                    .font(.system(size: 11))
                    .foregroundColor(Color.gray)
            }
            .frame(maxWidth: .infinity)
            .background(Color.white)
        }
        .listRowBackground(Color.gray)
        .frame(maxWidth: .infinity)
    }
}

结果是:

为什么列表中的每个元素不跨越整个屏幕? 填充物是从哪里来的?


共1个答案

匿名用户

则需要列表行背景

var body: some View {
    List {
        ForEach(fetcher.articles) { article in       // << needs ForEach !!
            VStack (alignment: .leading) {
                Text(article.section)
                    .foregroundColor(.red)
                    .textCase(.uppercase)
                Text(article.title)
                    .font(.system(size: 20))
                    .fontWeight(.semibold)
                UrlImageView(article.image)
                    .padding(EdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0))
                Text(article.author)
                    .font(.system(size: 11))
                    .foregroundColor(Color.gray)
            }
            .frame(maxWidth: .infinity)
        }.listRowBackground(Color.red)        // << here !!
    }
}