drawRect方法被调用但UIView无法显示

时间:2015-04-18 06:53:21

标签: ios objective-c iphone uiview core-graphics

ModeButtonView是UIView的子类,已经添加为viewController.view的子视图:

@interface ModeButtonView ()
@property(nonatomic, strong) MonthModeButtonView *monthModeButton;
@end

-(instancetype)initWithFrame:(CGRect)frame {
     self = [super initWithFrame:frame];
     if (!self) {
        return nil;
     }

    self.backgroundColor = [UIColor colorWithRed:0.192 green:0.663      blue:0.647 alpha:1];
    self.monthModeButton = [[MonthModeButtonView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.5 * self.bounds.size.width, 0.5 * self.bounds.size.height)];
    self.monthModeButton.center = self.center;
    [self addSubview:self.monthModeButton];
    return self;
}

MonthModeButtonView也是UIView的子类,它被添加为ModeButtonView的子视图。其drawRect方法如下:

- (void)drawRect:(CGRect)rect {
    NSLog(@"drawRect get called");
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y);
    CGPathAddLineToPoint(path, NULL, rect.size.width, rect.size.height);
    CGPathMoveToPoint(path, NULL, rect.size.width, rect.origin.y);
    CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.size.height);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGContextAddPath(currentContext, path);
    [[UIColor redColor] setStroke];
    CGContextDrawPath(currentContext, kCGPathStroke);

    CGPathRelease(path);
}

我运行程序,ModeButtonView在屏幕上显示良好,而MonthModeButtonView的drawRect方法也被调用,但是,它不能显示在屏幕上,而我已将其添加为ModeButtonView的子视图。

但是,如果我将MonthModeButtonView的drawRect方法的代码直接放入ModeButtonView的drawRect方法中,它就会显示出来。似乎问题发生是因为MonthModeButtonView是MoodeButtonView的子视图,但为什么呢?有什么想法吗?

0 个答案:

没有答案