【问题标题】:How do I change the cell backgroundcolor in SwiftUI and iOS 14?如何更改 SwiftUI 和 iOS 14 中的单元格背景颜色?
【发布时间】:2020-10-01 00:41:49
【问题描述】:

在我的 iOS 应用程序中,我有 SwiftUI 列表。在 iOS 14 之前,将其放入 AppDelegate 可以改变单元格的背景颜色:

UITableViewCell.appearance().backgroundColor = .black

在 iOS 14 之后,这不再改变颜色,它已恢复为默认的白色。

这也不起作用:

UITableViewCell.appearance().backgroundConfiguration?.backgroundColor = .black

在 iOS 14 的 SwiftUI 中是否有另一种方法可以更改 UITableViewCell 的颜色?

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您需要对单元格使用 listRowBackground 修饰符,并且仍然可以为整个表格视图背景使用外观,如下例所示

    使用 Xcode 12 / iOS 14 测试

    init() {
        UITableView.appearance().backgroundColor = UIColor.yellow
    }
    var body: some View {
        List {
            ForEach(0..<5, id: \.self) {
                Text("Hello \($0)").foregroundColor(.blue)
            }.listRowBackground(Color.black)
        }
    }
    

    【讨论】:

    • 这成功了!有没有办法改变分隔符的颜色?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2011-09-24
    相关资源
    最近更新 更多