为什么我无法更改 SwiftUI 中列表中的行背景

问题描述 投票:0回答:1

我不明白为什么 listRowBackground 只应用于没有子项的列表项,而不应用于可扩展的列表项?
这是它的样子:
列表

List(viewModel.tree, id: \.id, children: \.children) { tree in
                HStack{
                    Image(systemName:tree.children != nil ? "folder" : "antenna.radiowaves.left.and.right")
                        .fontWeight(.medium)
                    
                    Text(tree.name)
                        .fontWeight(.medium)
                        .textCase(.uppercase)
                        .padding(.vertical, 5)
                }
                .foregroundStyle(colorScheme == .light ? .black : Color.cGray)
                .listRowSeparator(.hidden)
                .onTapGesture {
                    if tree.children == nil {
                        changeView = true
                    }
                }
                .listRowBackground(colorScheme == .light ? .white : Color.dBackground)
            }
            .listRowSpacing(10)
            .listStyle(.plain)
            .background(colorScheme == .light ? .white : Color.dBackground)

不应该

listRowBackground(colorScheme == .light ? .white : Color.dBackground)
更改每个项目的背景吗?

ios swift swiftui swiftui-list
1个回答
0
投票

尝试这种方式,您将能够在我们管理部分标题时更改列表的背景颜色

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 ListTestView: View {

  let items: [Bookmark] = [.example1, .example2, .example3]

  @State
  var colorScheme: ColorScheme = .light
  @State
  var openSections: Set < UUID > = []

  var body: some View {
    VStack(content: {
      List(items, id: \.id) {
        row in
          Section {
            ForEach(row.items ? ? [], id : \.id) {
              item in
                if openSections.contains(row.id) {
                HStack {
                  Image(systemName: row.items != nil ? "folder" : "antenna.radiowaves.left.and.right")
                    .font(.subheadline)

                  Text(item.name)
                    .fontWeight(.medium)
                    .textCase(.uppercase)
                    .padding(.vertical, 5)
                }
                .foregroundColor(colorScheme == .light ? .black : Color.gray)
                  .listRowSeparator(.hidden)
                  .onTapGesture {
                    colorScheme = colorScheme == .light ? .dark : .light
                  }
              }
            }
          }
        header: {
            Text(row.name)
            .foregroundStyle(colorScheme == .light ? .black : .white)
            .onTapGesture {
              if openSections.contains(row.id) {
                openSections.remove(row.id)
              } else {
                openSections.insert(row.id)
              }
            }
          }
          .listRowBackground(colorScheme == .light ? .white : Color.black)
      }
      .listRowSpacing(10)
      .listStyle(.plain)
      .scrollContentBackground(.hidden)
      .background(colorScheme == .light ? .white : Color.black)
    })
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.