1

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

  • I dragged a uiview into storyboard and made it my CustomView class.
  • Then i changed the content mode to be left and set the background color to be pink color.
  • Then there's a button that just simply changes the custom view's width and height to be 1.5 times bigger.

Now, i found an interesting thing:

I run this, and click the button, then:

  • Case 1: If i override the draw rect method

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);
}
dknyxh
  • 31
  • 5
  • 1
    Update your question with relevant code. – rmaddy Oct 27 '16 at 05:19
  • @rmaddy Hi, i just updated the question. However i don't think the code will help anything. I'm just confused about the different behaviours with the UIView when contentMode set to left if i override drawRect method. – dknyxh Oct 27 '16 at 16:32
  • Is that the actual implementation of `drawRect:`? It's really empty? – rmaddy Oct 27 '16 at 16:34
  • @rmaddy Yes that is my implementation of drawRect. Because im just trying out stuff up. I also tried add [super drawRect:] but it still behaves differently. I think as long as I override drawRect, the custom view will behave differently from UIView (in this case is background color). I think this might have something to do with how background color is actually getting drawn in UIView. – dknyxh Oct 27 '16 at 19:39

1 Answers1

1

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

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
dknyxh
  • 31
  • 5