UIView子类initWithFrame没有被调用?
我有一个自定义的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];
}
,因为要初始化从它看起来像一个笔尖这种观点,所以它使用默认(的UIView的)initWithNibName:代替初始化使用initWithFrame:方法从笔尖加载
那么它是不可能覆盖子类中的初始化?另一个问题。在IB中,视图设置为50pt高,但是它的初始化高度为42pt。 – 2010-08-06 16:15:31
我会尝试覆盖init:以及看它是否被调用。我忘了UIView实际上并没有initWithNibName。这是UIViewController中的一个方法 – 2010-08-06 16:44:42
事情-(id)initWithCoder:(NSCoder*)coder
被inited
我希望人们会回来并将答案标记为正确。 – 2013-02-05 23:49:36
你可以在创建自定义视图的位置显示代码吗?你在调用: \t UIView * fred = [[UIView alloc] initWithFrame:myFrame]; – joelm 2010-08-06 18:31:50