如何在 SwiftUI 中修改列表的背景颜色? [英] How do I modify the background color of a List in SwiftUI?

查看:46
本文介绍了如何在 SwiftUI 中修改列表的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SwiftUI 中重新创建我使用 UIKit 构建的 UI,但我遇到了一些小问题.

I'm trying to recreate an UI I built with UIKit in SwiftUI but I'm running into some minor issues.

我想在这里更改 List 的颜色,但似乎没有任何属性按我的预期工作.下面的示例代码:

I want the change the color of the List here, but no property seems to work as I expects. Sample code below:

struct ListView: View {
    @EnvironmentObject var listData: ListData

       var body: some View {
        NavigationView {
            List(listData.items) { item in
                ListItemCell(item: item)
            }
            .content.background(Color.yellow) // not sure what content is defined as here
            .background(Image("paper-3")) // this is the entire screen 
        }
    }
}

struct ListItemCell: View {
    let item: ListItem

    var body: some View {

        NavigationButton(destination: Text(item.name)) {
            Text("\(item.name) ........................................................................................................................................................................................................")
                .background(Color.red) // not the area I'm looking for
        }.background(Color.blue) // also not the area I'm looking for
    }
}

推荐答案

好的,我找到了为列表行着色的解决方案:

Ok, I found the solution for coloring the list rows:

struct TestRow: View {

    var body: some View {
        Text("This is a row!")
        .listRowBackground(Color.green)
    }
}

然后在正文中:

List {
    TestRow()
    TestRow()
    TestRow()
}

这符合我的预期,但我还没有找到如何删除行之间的分隔线...

This works as I expect, but I have yet to find out how to then remove the dividing lines between the rows...

这篇关于如何在 SwiftUI 中修改列表的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆