I’m trying to draw decorate a UITableViewCell in my table view with a text banner that looks like a stamp, diagonally across the top left corner of the cell.
I’m probably doing this in the wrong place entirely, but I’m overriding -layoutSubviews to add the layer. I tried to do it in -drawRect: but the banner ends up covered by the table view’s imageView as the table renders (i.e. the layer is underneath the image view, as the image view is added later).
I’m really struggling to get the math right for this. I’ve calculated that, assuming the banner begins at 40 points from the top of the cell and 40 points from the left, angled at exactly -45º, the hypotenuse would be 56 points. So I’m making a CALayer 56 points wide, then rotating it -45º, which works. The problem is the position within the cell… it’s sitting way out into the cell, instead of hard up against the edges of it.
Rather than me apply trial and error to get this in the right place, can somebody help me with the math? Obviously I need to move the layer and rotate it.
It feels like anchorPoint is what I need here, but that seems to actually move the layer around, so I must be missing the point (no pun intended).
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(10, 10, 50, 50);
if (self.hasBanner) {
CALayer *banner = [CALayer layer];
banner.backgroundColor = [UIColor colorWithRed:.5f green:.5f blue:.5f alpha:1.f].CGColor;
banner.frame = CGRectMake(0, 40-15, 56, 15);
banner.anchorPoint = CGPointMake(0, 1); // this just makes it worse
banner.transform = CATransform3DMakeRotation(-45.0 / 180.0 * M_PI, 0.0, 0.0, 1.0);
[self.layer addSublayer:banner];
}
}

Where should the layer go?
Let’s draw a diagram:
We could go further and do some trig, but let’s stop here. It’s pretty obvious that the middle-top point of the banner should be at
(20,20). We can tell Core Animation to do exactly that.Positioning the layer
Think of it in terms of four separate steps:
These correspond to four properties:
bounds,anchorPoint,position, andtransform.You don’t want to touch the
frameproperty, because its value is derived from thebounds,position,transform, andanchorPoint. If you try to set theframe, CALayer will try to invert thetransform, apply that to the rect you gave, and then set theboundsandposition. That may not give the results you want, so you’re better off just ignoring it entirely — less confusing that way.(For more information, see the Core Animation Guide, specifically the section Layer Objects Define Their Own Geometry.)
In code, we will:
boundsto the rect0, 0, width, height. (I’m deliberately leavingwidthto you — it’s going to have to be more than 56.)anchorPointto the point0.5, 0. In other words, halfway along the width of the layer, and at the top of the layer.positionto the point20, 20.transformto rotate by 45°.By the way, in the code below, I’m setting the
affineTransforminstead of thetransform, just because it’s slightly more convenient for simple 2-D transformations.When to set up the layer
You’re correct that
-drawRect:is the wrong place to create and add layers. That method should draw into the content of the view (itsCGContext) but do nothing else.layoutSubviewswill work, but it will get called more often than you might expect, and you don’t want to create and add a new layer each time.It looks like you just need to set the layer’s geometry once, and never touch it again. Why not just create or destroy the layer when your
hasBannerproperty is changed?