AstroCB AstroCB - 8 months ago 47
iOS Question

Background color of a UITableView does not extend across the cell for iPad

I've set the background color of a UITableView and its cells to a dark blue in Interface Builder, which seems to work fine for all iPhone sizes:


However, on iPad (portrait and landscape), the color does not apply to the disclosure indicator (arrow) on the right of each cell:


I'd love to provide more details than that, but I'm not sure what else there is to say.

If I remove the cell accessory entirely, it fills the cell normally, but all of the other accessory options (checkbox, detail, etc.) have the same effect.

Any idea why?


It turns out that you cannot set the actual cell's background color from within Interface Builder (or, if you can, I couldn't find it).

So, the easiest way to do it is to set it programmatically from within your UITableView implementation methods:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    cell.backgroundColor = UIColor(red: 5.0/255, green: 51.0/255, blue: 103.0/255, alpha: 1.0)

What tripped me up for a while with the RGB values is that you have to divide them by 255 (if you don't, it will just show up as white).