I have a basic Cocoa app with a custom NSCollectionView
that overrides drawRect:
to draw a red background (for illustration purposes).
I noticed that at seemingly random times, drawRect:
would not be called although I invoked setNeedsDisplay:YES
. Other times it worked and the collection view had a red background.
It took me a few hours, but then I finally noticed that high up in the view hierarchy one of the parent views was a layer-backed view (I had "Core Animation Layer" checked in Interface Builder to add a shadow to an element).
Changing that parent view to not use layer-backing immediately solved the problem and drawRect:
is then always called as expected.
What puzzles me is that with layer-backing enabled on a parent view, drawRect:
would be called sometimes, but not always.
Should I assume that I can't reliably override drawRect:
in any subview if a parent view is layer-backed?
Watch the WWDC 2012 video on layer-backed views, and read the documentation for -layerContentsRedrawPolicy
. Sounds like you want NSViewLayerContentsRedrawOnSetNeedsDisplay
.