如何在 SwiftUI 中更改可折叠列表的 listRowBackground

Syl*_*lar 3 swiftui

我有一个类似或相同的问题答案绝对不是答案。

使用 Hacking 与 Swift 的例子

struct Bookmark: Identifiable {
    let id = UUID()
    let name: String
    let icon: String
    var items: [Bookmark]?

    // some example websites
    static let apple = Bookmark(name: "Apple", icon: "1.circle")
    static let bbc = Bookmark(name: "BBC", icon: "square.and.pencil")
    static let swift = Bookmark(name: "Swift", icon: "bolt.fill")
    static let twitter = Bookmark(name: "Twitter", icon: "mic")

    // some example groups
    static let example1 = Bookmark(name: "Favorites", icon: "star", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example2 = Bookmark(name: "Recent", icon: "timer", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
    static let example3 = Bookmark(name: "Recommended", icon: "hand.thumbsup", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
}

struct ContentView: View {
    let items: [Bookmark] = [.example1, .example2, .example3]

    var body: some View {
        List(items, children: \.items) { row in
            Image(systemName: row.icon)
            Text(row.name)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

列表应该是可折叠的并具有更改的能力listRowBackground。这可能吗?我现在想知道是否Bookmark.items应该使用修饰符来查看某些视图.listRowBackground(..)

Asp*_*eri 6

根据listRowBackground接口约定:

var body: some View {
    List {
        OutlineGroup(items, children: \.items) { row in
            HStack {
                Image(systemName: row.icon)
                Text(row.name)
            }
        }.listRowBackground(Color.red)
    }
}
Run Code Online (Sandbox Code Playgroud)

其他一切与参考问题/答案中的相同。

使用 Xcode 13.4 / iOS 15.5 进行测试

演示