【问题标题】:SwiftUI - How to change the background of a list in edit modeSwiftUI - 如何在编辑模式下更改列表的背景
【发布时间】:2020-12-17 06:15:04
【问题描述】:

我想更改列表在.editMode 中时的背景颜色。到目前为止,我尝试的是通过以下方式初始化视图来更改列表的.background 的一般方法:

init() {
        UITableView.appearance().backgroundColor = .clear
        UITableView.appearance().separatorColor = .none
        UITableView.appearance().separatorStyle = .none
    }

并使用.background 修饰符设置列表的背景颜色。 这是有效的,但是一旦在.editMode 中显示Color(.systemsBackground)(黑色)而不是我的自定义ColorPicture of List in editMode)。

我的列表代码是:

List {
            ForEach(categoryVM.selectedCategories) { category in
                NavigationLink(destination: EditCategoryColorView(category: category, editType: .edit, name: category.name, color: category.color)){
                    ZStack {
                        HStack {
                            Text("\(category.name)")
                            Spacer()
                            Color("\(category.color)")
                                .aspectRatio(1, contentMode: .fit)
                                .cornerRadius(6)
                                .frame(width: 40, height: 40)
                        }
                    }
                }.padding(3)
            }.onDelete(perform: categoryVM.deleteCategory)
            .onMove(perform: move)
            .foregroundColor(colorScheme == .dark ? Color.white : Color.black)
            .background(colorScheme == .dark ? Color("DarkGrayApp") : Color.white)
            .listRow()
           
        }
        .padding(.vertical)
// to enable edit mode
            EditButton()

在代码中.listRow() 是一个扩展,所以行填充了整个可用空间。

extension View {
    func listRow() -> some View {
        self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
            .listRowInsets(EdgeInsets(top: -1, leading: -1, bottom: -1, trailing: -1))
            .background(Color(.systemBackground))
    }
}

我也尝试更改扩展程序中的颜色,但没有成功。

知道如何在.editMode 中更改一次背景颜色吗?

谢谢!

【问题讨论】:

    标签: list background swiftui edit


    【解决方案1】:

    只是更新:.listRowBackground(Color.clear) 为我解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多