I have a prototype cell with a custom class that I want to draw a few extra layers on the cell once when the cell is first initialized and not every time the cell is reused. In the past I would have done this by implementing awakeFromNib. I want to be able to access the frame of the views in my cell so I can use their dimensions in my new layer drawings, but with iOS6 the subviews all have frame width/height of 0 in the awakeFromNib method. I suspect it has to do with the new constraints layout stuff which I don't really understand yet.
- (void)awakeFromNib {
[super awakeFromNib];
// We only want to draw this dotted line once
CGPoint start = CGPointZero;
CGPoint end = CGPointMake(self.horizontalSeparator.frame.size.width, 0);
// Category that creates a layer with a dotted line and adds it to the view.
[self.horizontalSeparator addDottedLine:start to:end];
}
In awakeFromNib the horizontalSeparator.frame = (0 100; 0 0). How can I draw this dotted line layer once per cell and use the width of the existing horizontalSeparator view to determine the length of the line?
UPDATE
I figured out that I can use the constraints on the superview to figure out the dimensions on the subviews, but I'm still hoping someone can point me towards a better solution that doesn't make assumptions about the constraint configuration.
for (NSLayoutConstraint *constraint in self.constraints) {
if (// Find a constraint for the horizontalSeparator
(constraint.firstItem == self.horizontalSeparator
|| constraint.secondItem == self.horizontalSeparator)
&& // Make sure it affects the leading or trailing edge.
(constraint.firstAttribute == NSLayoutAttributeLeading
|| constraint.firstAttribute == NSLayoutAttributeTrailing)) {
CGFloat margin = constraint.constant;
CGPoint start = CGPointZero;
CGPoint end = CGPointMake(self.frame.size.width - (2 * margin), 0);
[self.horizontalSeparator addDottedLine:start to:end];
_isInitialized = YES;
break;
}
}