【发布时间】:2011-05-27 18:56:00
【问题描述】:
我有一个非常简单的 UIView 自定义子类:
#import "BarView.h"
#import <QuartzCore/QuartzCore.h>
@implementation BarView
@synthesize barColor;
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawRect");
// Draw a rectangle.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, self.barColor.CGColor);
CGContextBeginPath(context);
CGContextAddRect(context, self.bounds);
CGContextFillPath(context);
}
- (void)dealloc
{
self.barColor = nil;
[super dealloc];
}
@end
如果我用一些非零矩形在这个类上调用 initWithFrame:rect,它工作正常;但是当我调用 initWithFrame:CGRectZero 时,drawRect 永远不会被调用,即使在我将视图的框架修改为非零矩形之后也是如此。
我当然明白为什么一个零帧的视图永远不会调用它的 drawRect:,但为什么即使在帧改变后它也不会调用呢?
【问题讨论】: