If I set the backgroundColor attribute on a grouped UITableViewCell, the background color successfully changes. Great.
But I’d like to use UIAppearance to change the background color on all my UITableViewCells, so I can do it in one place and affect a change everywhere. Here’s my code:
[[UITableViewCell appearance] setBackgroundColor:[UIColor colorWithRed:30.0/255.0 green:30.0/255.0 blue:30.0/255.0 alpha:1.0]];
UITableViewCell implements UIAppearance and UIAppearanceContainer, so I would have thought this would work. But it doesn’t. I’ve also tried using -[UITableViewCell appearanceWhenContainedIn:(Class)], and that doesn’t work either.
Any ideas?
Update (2013/7/8) – This has been fixed in newer versions of iOS. However, it’s worth knowing about if you’re targeting iOS 6 or below.
You can blame Apple for this one, and it’s actually pretty mean of them. Technically,
backgroundColoris not customizable through appearance proxies.From Apple’s documentation:
If we go into a class like
UIBarButtonItemand look at thetintColorproperty we see this:So because it’s marked with the
UI_APPEARANCE_SELECTORtag we know it works withUIAppearance.Here’s where Apple are particularly mean: in a
UIView,backgroundColorhas no appearance selector tag, but still works withUIAppearance. According to all the documentation Apple provide it should not, but yet it does!This gives the misleading impression that it will work for all sub-classes of
UIView, includingUITableView. This has come up before, in this previous SO answerSo the bottom line is that
backgroundColorshouldn’t work at all withUIAppearance, but for some reason it does on aUIView. It is not guaranteed to work onUIViewsubclasses, and it doesn’t work at all onUITableView. Sorry I couldn’t give you a more positive answer!