Reputation: 3601
I'm using CALayers to draw to a UITableViewCell. I'm trying to figure out how layers are ordered with the content of the UITableViewCell. For instance:
So what would be the order of these elements. I know I have zPosition on the CALayers but I'm not sure if they are always on top of any subviews of the UITableViewCell. And I'm not sure where the content that is drawn in drawRect comes in the order. Any help or links to documentation would be great. I have read through the Core Animation Programming Guide and didn't see anywhere where this would be answered.
Upvotes: 3
Views: 5064
Reputation: 4363
Note that drawing in a UITableViewCell and using child views at the same time isn't recommended for performance reasons. Having more than 4 child views/layers isn't recommended for the same reason.
Upvotes: 3
Reputation: 170319
CALayers maintain a hierarchy, just like views do. Sublayers render in front of their superlayer, all the up the hierarchy. The zPosition
property should only be used to control the rendering of sibling layers by placing one above the other. Even then, it's preferred that you order the sibling layers within the sublayers
property (the layers draw in the order they are within that array, with each successive layer drawn upon the one before it in the array).
It's pretty easy to see this hierarchy, because you are manually adding sublayers to a layer when you create them in your code, all the way back to the backing layer for your UITableViewCell (a subclass of the layer-backed UIView).
As an aside, you really should not be adding layers to your UITableViewCell within its -drawRect:
method. That should only be used for drawing the content of the base layer of the UITableViewCell. Allocating and setting CALayers within this method may lead to significant slowdown while scrolling or redrawing. You should only need to set up these layers once at some point when the UITableViewCell is initialized, then simply update them as the cell is reused.
Upvotes: 4