Jesse Earle Jesse Earle - 3 months ago 27
iOS Question

When I subclass UIView and implement an empty drawRect method, the colorWithPatternImage image resizes to fill the view, why?

The background property is an outlet in IB for a regular UIView.

UIImage* image = [UIImage imageNamed:@"glow.jpg"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 768), NO, 0.0);
[image drawInRect:CGRectMake(0, 0, 1, 768)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
self.background.backgroundColor = [UIColor colorWithPatternImage:newImage];

If I don't add a custom class to self.background, the glow doesn't resize itself when the view resizes it self (it's a gradient), it repeats at the 768 height.

However if I subclass UIView, set the background property to that class and override the drawRect method, it does resize the pattern image to be the height of the view.

Very weird, wondering if anyone can explain this?


iOS: Using UIView's 'drawRect:' vs. it's layer's delagate 'drawLayer:inContext:'


    // UIView uses the existence of -drawRect: to determine if it should allow its CALayer
    // to be invalidated, which would then lead to the layer creating a backing store and
    // -drawLayer:inContext: being called.
    // By implementing an empty -drawRect: method, we allow UIKit to continue to implement
    // this logic, while doing our real drawing work inside of -drawLayer:inContext:


-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context