tranvutuan
tranvutuan

Reputation: 6109

getting weird result at redraw method when subclass UIView, Objective C

My TNContainer is subclassed of UIView and I am doing like the following to drawRect method

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationBar setBackgroundImage:[UIImage imageNamed: @"UINavigationBarBlackOpaqueBackground.png"]
                                       forBarMetrics:UIBarMetricsDefault];
    self.containerView      =   [[TNContainer alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];
    self.navigationBar.topItem.titleView    =   self.containerView;

}

in my TNContainer.m, I am doing ( putting some statements to print out the bounds and frame)

#import "TNContainer.h"

@implementation TNContainer

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    NSLog(@"frame at initWithFrame is %@", NSStringFromCGRect(self.frame));
    NSLog(@"bound at initWithFrame is %@", NSStringFromCGRect(self.bounds));
    return self;
}

-(void) drawRect:(CGRect)rect{
    [super drawRect:rect];
    NSLog(@"frame at drawRect is %@", NSStringFromCGRect(self.frame));
    NSLog(@"bounds at drawRect is %@", NSStringFromCGRect(self.bounds));

    CGRect rectangle        = self.bounds;
    CGContextRef context    = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rectangle);
}

It is very interesting that the bounds of the frame in initWithFrame and drawRect are totally different

2013-09-25 16:19:47.926 TNSegmentController[10458:a0b] frame at initWithFrame is {{0, 0}, {100, 10}}
2013-09-25 16:19:47.928 TNSegmentController[10458:a0b] bound at initWithFrame is {{0, 0}, {100, 10}}
2013-09-25 16:19:47.934 TNSegmentController[10458:a0b] frame at drawRect is {{110, 17}, {100, 10}}
2013-09-25 16:19:47.934 TNSegmentController[10458:a0b] bounds at drawRect is {{0, 0}, {100, 10}}

Why I am getting {{110, 17}, {100, 10}} for the frame in drawRect()....

Any ideas about this ?

Upvotes: 0

Views: 137

Answers (2)

DoS
DoS

Reputation: 1474

because "bounds" are in relation to itself. "frame" is a reference to its location in the super view. as @Tim mentions, if the super view changes the location, the "frame" value will change but the internal "bounds" will not, assuming the super view did not adjust the size.

Upvotes: 0

Tim
Tim

Reputation: 60150

That's not strange at all - in fact, it's to be expected. At -initWithFrame: time, you pass in a frame with origin (0,0). That frame is duly logged in your UIView subclass.

By the time that view goes to draw, however, you've assigned it to be the titleView of a navigation bar; the bar has likely added your view as its own subview and repositioned it to be centered. Your first drawing request doesn't come in until after all that has happened, and it's why you see a different origin - (110,17) - in your -drawRect: override.

(You'll note that your original size is preserved, which I imagine is the thing you really care about. Changing the origin of the view - and therefore the frame - is the desired behavior for using a custom title view.)

Upvotes: 2

Related Questions