views:

103

answers:

2

I have a custom UIView subclass. In IB, I have specified a "placeholder" UIView and set the class to my class name. My override of drawRect method is working, and the background is coloring properly, but the initWithFrame is not firing. Why?

- (id)initWithFrame:(CGRect)frame {
    NSLog(@"initWithFrame");
    if ((self = [super initWithFrame:frame])) {
        noteTextFont = [[UIFont boldSystemFontOfSize:12] retain];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *backgroundColor = [UIColor blackColor];
    [backgroundColor set];
    CGContextFillRect(context, rect);

    UIColor *labelColor = [UIColor lightGrayColor];
    [labelColor set];
    [notificationLabel drawInRect:CGRectMake(44, 18, rect.size.width-20, rect.size.height) withFont:noteTextFont lineBreakMode:UILineBreakModeTailTruncation];

    [self setNeedsLayout];
}
A: 

because you are initializing this view from a nib it looks like, so it is using the default (UIView's) initWithNibName: instead of initializing using initWithFrame:

Jesse Naugher
So is it then impossible to override the initialization in the subclass? Another question. In IB, the view is set to be 50pt tall, but it's being initialized with a frame that is 42pt tall.
E-Madd
i would try overriding init: as well and see if it gets called. I forgot that UIView does not actually have initWithNibName. that is a method in UIViewController
Jesse Naugher
A: 

Things loaded from a nib are inited with -(id)initWithCoder:(NSCoder*)coder

tc.