【问题标题】:Can't get view full screen in SwiftUI无法在 SwiftUI 中全屏查看
【发布时间】:2020-06-24 22:46:25
【问题描述】:

我正在尝试在 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)
    }
}

结果如下:

为什么列表中的每个元素都没有跨越整个屏幕?填充从哪里来?

【问题讨论】:

    标签: ios swiftui


    【解决方案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 !!
        }
    }
    

    【讨论】:

    • 我已经编辑了我的代码,现在我遇到了类似的问题。我仍然无法让内容横跨整个屏幕。
    • @BryanDeemer,我想你应该在同一级别添加 .listRowInsets(EdgeInsets()) 修饰符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多