ios - iOS 13 中 UITableViewCell 的默认背景色是什么?

标签 ios uitableview ios13

什么是默认背景 UIColorUITableViewCell ?我需要一个常数 UIColor对象而不是 RGB,因为我希望在 iOS 13 中实现暗模式。(我找不到任何匹配的颜色,例如 [UIColor systemBackgroundColor] )。

我在 willDisplayCell:forRowAtIndexPath: 放置了一个断点并打印 cell.backgroundColor .这就是我得到的:

<UIDynamicSystemColor: 0x600000bf2c00; name = tableCellGroupedBackgroundColor>

它似乎是一个没有公共(public)等价物的私有(private)类(class)。有什么建议可以针对这个吗?

最佳答案

普通样式 TableView 中的单元格使用 UIColor.systemBackground[Color]对于他们的背景,UIColor.label[Color]标题文本,UIColor.secondaryLabel[Color]为字幕文本。

对于分组样式 TableView ,单元格背景使用 UIColor.secondarySystemGroupedBackground[Color]并且表格 View 背景使用 UIColor.systemGroupedBackground[Color] .

所有这些都适应明暗模式。

下面是一个有用的 UIColor 扩展,它允许您打印任何颜色的明暗描述。

extension UIColor {
    var lightDarkDescription: String {
        let lightTraits = UITraitCollection.init(userInterfaceStyle: .light)
        let darkTraits = UITraitCollection.init(userInterfaceStyle: .dark)
        let lightColor = self.resolvedColor(with: lightTraits)
        let darkColor = self.resolvedColor(with: darkTraits)
        if lightColor == darkColor {
            return self.description
        } else {
            return "\(self), light: \(lightColor), dark: \(darkColor)"
        }
    }
}

例子:
print(UIColor.secondarySystemGroupedBackground.lightDarkDescription)
print(UIColor.secondaryLabel.lightDarkDescription)
print(UIColor.green.lightDarkDescription)

输出:

<UIDynamicSystemColor: 0x6000005a5d80; name = secondarySystemGroupedBackgroundColor>, light: UIExtendedGrayColorSpace 1 1, dark: UIExtendedSRGBColorSpace 0.109804 0.109804 0.117647 1
<UIDynamicSystemColor: 0x6000005a5f00; name = secondaryLabelColor>, light: UIExtendedSRGBColorSpace 0.235294 0.235294 0.262745 0.6, dark: UIExtendedSRGBColorSpace 0.921569 0.921569 0.960784 0.6
UIExtendedSRGBColorSpace 0 1 0 1



如果有人想玩所有颜色,请参阅我的 SystemColors GitHub 上的演示应用程序。

关于ios - iOS 13 中 UITableViewCell 的默认背景色是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186481/

相关文章:

ios - 如何在UINavigationBar中自定义导航栏标题字体和大小?

iphone - 如何从 iphone 相机胶卷中获取相册列表

ios - tableView.deleteRows 后缺少随机 UITableViewCell

ios - 如何快速在 TableView 的不同多个部分中添加多行

ios - 在 didSelectRowAtIndexPath 方法中获取所选单元格的内容

SwiftUI : Error while using if#available while trying to build to iOS 13 and iOS 14 in Xcode 12 beta 2

iphone - 我应该购买哪个 iOS 开发者程序?

uilocalnotification - iOS 13 本地通知未发送

ios - 结合框架第三个 `collect`方法

ios - 访问单例中的非可选属性时出现无法解释的崩溃