1

I've experienced some really weird behavior with UILabels in a UICollectionViewCell today and I'm hoping one of you guys can shed a light on this.

I can't show you code or full screenshots, but I will try to explain and illustrate it as well as possible;

I have a UICollectionView that has several cells and supports horizontal scrolling. In the cells I have a label that I set on - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath and clear on - (void)prepareForReuse.

Seems pretty normal, and I even have an implementation exactly like this that works without any weirdness.

Now, for performance reasons, I set the UILabel's opaqueness ON in Interface Builder. When I scroll around a couple of times, I get to see this:

weird rendering behavoir

Whenever I turned opaque OFF in Interface Builder, I get to see this: enter image description here

I have a lot of experience with UITableViews and UICollectionViews (and reusing, etc) but there is no way I can explain this behavior properly...

Jake
  • 3,973
  • 24
  • 36
  • seems like you are stacking two labels. – Jonathan Cichon Apr 11 '13 at 09:56
  • @JonathanCichon how? I placed a label via IB and reusing it (as described.) Even if I were stacking, how is opaqueness ON/OFF going to cause this (I would even suspect it to be the other way around if that were the case.) – Jake Apr 11 '13 at 10:08
  • 1
    if you are using opague (not alpha) the context of the label is not guaranteed to be cleared before redraw. So you might end up in seeing the previous text behind the new text. Opague makes only sens with a custome drawRect implementations which takes advantage of the old context or fills the entire screen anyways. – Jonathan Cichon Apr 11 '13 at 21:36
  • I'm still not 100% convinced (sources?) but I'll take it. – Jake Apr 15 '13 at 10:30
  • form the apple documentation: **An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable.** – Jonathan Cichon Apr 15 '13 at 12:14
  • thanks for the citation, as I've never encountered this before; how does this apply to an opaque UILabel? (it's opaque, it fills its bounds and it doesn't (?) contain transparent content) – Jake Apr 15 '13 at 13:31

1 Answers1

0
yourLabel.backgroundColor = UIColor.whiteColor()

UILabel's default background color is clear color. If you set the label to opaque, you also need to set the background color to an opaque color.

Jonny
  • 1,969
  • 18
  • 25