問題描述
我有一個自定義的分組 UITableViewCell,上面有幾個 UILabel.由于 UITableViewCell 的背景顏色曾經是純白色,它與 UILabel 的默認背景顏色匹配,因此 UILabel 框不可見..
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..
更新到 iOS 5.0 后,我注意到現在分組 UITableViewCells 的默認背景顏色是更灰白色(實際上是 #f7f7f7),因此 UILabels 的框架以丑陋的方式可見..
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..
那么,當需要在不同 iOS 版本之間變化時,設置 UILabel 的背景顏色的最佳方法是什么?我知道我可以使用 opaque = NO 和 [UIColor clearColor],但出于性能原因,我更喜歡繪制 UILabel 的背景.
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.
推薦答案
在委托方法 tableView:willDisplayCell:
中,UITableViewCell
將背景顏色設置為白色或者,在 iOS 5 中,為灰色.
In the delegate method tableView:willDisplayCell:
, the UITableViewCell
will have the background colour set to white or, in iOS 5, greyish.
您可以更改所有子視圖的所有 backgroundColor
.
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;
}
}
這篇關于不同的單元格背景顏色取決于 iOS 版本(4.0 到 5.0)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!