我有一个自定义的UIView子类。在IB中,我指定了一个“占位符”UIView并将该类设置为我的类名。我的重写drawRect方法正在工作,并且背景着色正确,但initWithFrame没有触发。为什么?UIView子类initWithFrame没有被调用?
- (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];
}
你可以在创建自定义视图的位置显示代码吗?你在调用: \t UIView * fred = [[UIView alloc] initWithFrame:myFrame]; – joelm 2010-08-06 18:31:50