Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different cell bg color depending on iOS version (4.0 to 5.0)

I have a custom grouped UITableViewCell, with a couple of UILabels on it. Since the UITableViewCell background color used to be pure white, it matched the UILabels' default background color, so the UILabel box wasn't visible..

After updating to iOS 5.0, I notice that now the default background color for grouped UITableViewCells is a more greyish white (actually #f7f7f7), and as a consequence the UILabels' frame is visible in an ugly way..

So, what is the best way to set the UILabels' background color when it needs to vary between different iOS versions? I know I could use opaque = NO and [UIColor clearColor], but I would prefer to paint the UILabels' background for performance reasons.

like image 749
yannis Avatar asked Oct 11 '11 23:10

yannis


1 Answers

In the delegate method tableView:willDisplayCell:, the UITableViewCell will have the background colour set to white or, in iOS 5, greyish.

You can change all your the backgroundColor of all your subviews.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    for (UIView* view in cell.contentView.subviews) {
        view.backgroundColor = cell.backgroundColor;
    }
}
like image 99
gcamp Avatar answered Oct 22 '22 12:10

gcamp