开发者

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 a开发者_如何转开发nd [UIColor clearColor], but I would prefer to paint the UILabels' background for performance reasons.


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;
    }
}


Nothing wrong with gcamp's answer, but if you'd prefer to keep the background white on both iOS4 and iOS5, then just do this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setBackgroundColor:[UIColor whiteColor]];
}


I saw that problem in my custom UITableViewCell in a grouped TableView I just set the label background color to clear and that fixed it for me and works in iOS4 and iOS5. I set this in the custom cell code where the UILabel gets created as below:

    [nameLabel setBackgroundColor:[UIColor clearColor]];

After that problem gone.


You can call:

 [[UIDevice currentDevice] systemVersion]

Although that is discouraged for a lot of reasons, and I'm not sure it's the best way to solve your problem. In principle, you should be able to deploy view code and get the consistent results you want by some other means.

If you do actually want to compare device versions, you'd probably set a property and check the device's OS when the view controller loads, as opposed to within your cellForRowAtIndexPath...


What I did was set the background color of the cell to [UIColor whiteColor] in willdisplaycell...

That way I control how things look.


its use UIColor.systemBackground but its available for IOS 13 Only so use it likes this :

if #available(iOS 13.0, *) {

  cell.backgroundColor = UIColor.systemBackground

} else {

  cell.backgroundColor = .white

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜