Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default background color of UITableViewCell in iOS 13?

What is the default background UIColor of UITableViewCell? I need a constant UIColor object rather than RGB as I wish to implement dark mode in iOS 13. (I couldn't find any matching color such as [UIColor systemBackgroundColor]).

I placed a breakpoint at willDisplayCell:forRowAtIndexPath: and printed cell.backgroundColor. This is what I got:

<UIDynamicSystemColor: 0x600000bf2c00; name = tableCellGroupedBackgroundColor>

It seems like a private class with no public equivalent. Any suggestions how to target this?

like image 934
Joshua Avatar asked Jul 24 '19 15:07

Joshua


People also ask

What is the default background color of a cell?

By default the background color of the cell is white in MS Excel.

How do I change the background color in Xcode?

At the top select the attributes inspector. Under the section "View" there should be a spot that says "Background". click that and choose your colour.


1 Answers

Cells in a plain styled table view use UIColor.systemBackground[Color] for their background, UIColor.label[Color] for the title text, and UIColor.secondaryLabel[Color] for the subtitle text.

For a grouped style table view, the cell background uses UIColor.secondarySystemGroupedBackground[Color] and the table view background uses UIColor.systemGroupedBackground[Color].

All of these adapt to light/dark mode.

Below is a helpful UIColor extension that allows you to print the light and dark description of any color.

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)"
        }
    }
}

Examples:

print(UIColor.secondarySystemGroupedBackground.lightDarkDescription)
print(UIColor.secondaryLabel.lightDarkDescription)
print(UIColor.green.lightDarkDescription)

Output:

<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

If anyone wants to play with all of the colors, see my SystemColors demo app on GitHub.

like image 174
rmaddy Avatar answered Sep 21 '22 07:09

rmaddy