UIView子类initWithFrame没被调用?

E-M*_*add 8 iphone cocoa-touch

我有一个自定义的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];
}
Run Code Online (Sandbox Code Playgroud)

tc.*_*tc. 52

从笔尖装入的东西都是由 -(id)initWithCoder:(NSCoder*)coder

  • 我希望人们回来并将答案标记为正确. (13认同)

Jes*_*her -2

因为您是从笔尖初始化此视图,所以它使用默认的(UIView 的) initWithNibName: 而不是使用 initWithFrame 初始化: