Basically, this works because the mechanism that sets up the graphics context for drawing, etc., does not live in the implementation of UIView -drawRect: (empty).
This mechanism really lives somewhere, of course, let it pretend that in every UIView there is such an internal method as this: the names of methods, functions and properties are invented to protect the innocent:
- (void)_drawRectInternal:(CGRect)rect { // Setup for drawing this view. _UIGraphicsContextPushTransform(self._computedTransform); // Call this object implementation of draw (may be empty, or overridden). [self drawRect:rect]; // draw our subviews: for (UIView * subview in [self subviews]) { [subview _drawRectInternal:rect]; } // Teardown for this view. _UIGraphicsContextPopTransform(); }
This is a cleaner way to build frameworks than relying on a subclass to do the right thing in a general redefinition, where misuse would lead to very mysterious bad behavior. (And that would not be really possible, since drawing and the tear-off step might be required for drawing.)
Ben zotto
source share