我有一个自定义的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];
}

最佳答案

从 Nib 加载的内容由-(id)initWithCoder:(NSCoder*)coder初始化

10-08 15:42