dknyxh
dknyxh

Reputation: 31

UIView content mode acts differently when override drawRect:

Im learning iOS. I've been reading Apple's View programming guide. I did the followings to try out content mode:

Now, i found an interesting thing:

I run this, and click the button, then:

drawRect: overrided

Then the appearance of the view shifted down

The size actually increased

After doing some search online and look into Apple's document, i couldnt any relavent things except one question mention that this might have something to do with drawLayer:inContext: behave differently if override the drawRect:

Does anyone know what is going on here?

(sorry about the formatting, this is a new account and i can't post more than 2 links.)

CODE: For the CustomView, just either override drawRect or without it.

#import "CustomView.h"
@implementation CustomView
- (void)drawRect:(CGRect)rect{
//Do nothing, for case 2 i just commented out this method.
}
@end

For Changing the frame of customView:

- (IBAction)changeBound:(id)sender {
    self.customView.frame = CGRectMake(self.customView.frame.origin.x, self.customView.frame.origin.y, self.customView.frame.size.width * 1.5, self.customView.frame.size.height * 1.5);
}

Upvotes: 1

Views: 141

Answers (1)

dknyxh
dknyxh

Reputation: 31

After doing some research, i think i figure out what happened:

So drawing background code is actually handled separately in -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context. That's why with empty drawRect, it still draws the background. Also, depending on the existence of drawRector not(Probably by calling respondsToSelector?), UIView behave different in drawLayer :inContext. See Dennis Fan's answer in this link

Upvotes: 1

Related Questions