相关疑难解决方法(0)

如何在SwiftUI中修改列表的背景色?

我正在尝试重新创建使用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)

swift swiftui

17
推荐指数
13
解决办法
5227
查看次数

iOS SwfitUI 设置列表而不是单元格的背景图像

我试图为我的 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,但我认为该列表覆盖了我的图像

那么如何将图像作为列表背景或视图背景(上面的代码是整个视图)

ios swift swiftui swiftui-list swiftui-navigationlink

4
推荐指数
1
解决办法
2232
查看次数