我正在尝试重新创建使用SwiftUI中的UIKit构建的UI,但是遇到了一些小问题。
我想要更改List此处的颜色,但是没有任何属性像我期望的那样起作用。下面的示例代码:
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 …Run Code Online (Sandbox Code Playgroud) 我试图为我的 ListView 提供一个背景图像,该图像不应适用于每个单元格。因此整个视图只需一张图像。我尝试添加.background(Image("nameOfImage))为列表修饰符但没有执行任何操作。
NavigationView {
List(elements) { element in
NavigationLink(destination: NextView) {
Text(element.name)
Image(element.image)
}
}.background(Image("nameOfImage"))
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过 ZStack,但我认为该列表覆盖了我的图像
那么如何将图像作为列表背景或视图背景(上面的代码是整个视图)