我有一个自定义的UIView子类。在IB中,我指定了一个“占位符”UIView并将该类设置为我的类名。我对drawRect方法的重写是有效的,并且背景正确着色,但是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];
}
答案 0 :(得分:52)
从笔尖加载的东西都是-(id)initWithCoder:(NSCoder*)coder
答案 1 :(得分:-2)
因为你正在从它看起来的nib初始化这个视图,所以它使用默认的(UIView的)initWithNibName:而不是使用initWithFrame进行初始化: