开发者

iPhone CALayer Stacking Order

I'm usin开发者_开发问答g CALayers to draw to a UITableViewCell. I'm trying to figure out how layers are ordered with the content of the UITableViewCell. For instance:

  1. I add labels to the UITableViewCell in my cellForRow:atIndexPath method
  2. In the drawRect method of UITableViewCell I draw some content using the current context
  3. Also, in drawRect I add a few sublayers

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.


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.


  1. Your UITableViewCell (or derived class) derives from UIView. It owns one CALayer which you can access via the layer property. This layer takes up the whole area of the view. The UILabels you add to your cell view will be children of your cell's UIView and their CALayer will be a child of the cell's CALayer. Child layers are drawn in front of their parents.
  2. When you draw anything in UIView's drawRect method, the CALayer the UIView owns will become backed by a bitmap. Your UILabels will obscure this bitmap partially or fully depending on how big they are.
  3. Any child CALayers you add to your cell's layer later will be siblings of the UILabel's layers and since you added them later they will be added under them by default (there are versions of the method that adds them which can control ordering though).

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜