什么是默认背景 UIColor
的 UITableViewCell
?我需要一个常数 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/