简体   繁体   中英

Setting the Background Color of a UITableViewCell to a Pattern Image

I have the follow code which sets the background of a UITableViewCell to a pattern image:

UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TestPattern"]];
cell.backgroundColor = color;

This works and the background pattern is displayed, but the pattern overlaps strangely:

在此处输入图片说明

I think this is because the UITableViewCell is actually setting the background color of the labels and other views it contains along with its main view. How can I get the background color to not overlap?

I think this is because the UITableViewCell is actually setting the background color of the labels

Correct, and very well explained. So you have to set the background color of the labels to clearColor separately. This is not entirely easy because, depending on the version of iOS, the runtime will try to set it back again for you; in iOS 4, for example, it was necessary to make this move in willDisplayCell , though in iOS 6 you can get away with doing it in cellForRow as long as you do it after setting the background color.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM