本文介绍了UIView子类initWithFrame未被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UIView子类。在IB中,我指定了一个占位符UIView并将类设置为我的类名。我的覆盖drawRect方法是工作,背景正在着色,但initWithFrame不是发射。为什么?

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];
}


推荐答案

inited with - (id)initWithCoder:(NSCoder *)coder

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

这篇关于UIView子类initWithFrame未被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:23