iOS 5 UIView drawRect覆盖不在设备上工作

时间:2011-10-08 20:34:04

标签: iphone ios uiview drawrect

我正在准备我的iPhone应用程序在iOS 5 GM上发布,并遇到了UIView的错误。当我在子类上覆盖drawRect方法时,模拟器会显示所需的结果,但是当我尝试在实际设备上进行测试时,drawRect覆盖根本没有任何效果。

我甚至在drawRect覆盖中放置了一个日志语句,并确认它正在被调用。

有没有人注意到这个问题?

这是我正在使用的覆盖代码:

- (void)drawRect:(CGRect)rect {
DebugLog( @"*** calling drawRect ***" );

CGContextRef context = UIGraphicsGetCurrentContext(); 

// draw the dark gray background
CGContextSetFillColor(context, [SkinManager getWhiteColor]);
CGContextFillRect(context, CGRectMake(0.0, 0.0, rect.size.width, rect.size.height)); 

// draw the text field background in white
CGContextSetFillColor(context, [SkinManager getDarkGray]);
CGContextFillRect(context, CGRectMake(2.0, 2.0, rect.size.width - 4, rect.size.height - 4)); 

}

这是生成颜色的代码

    +(const CGFloat*) getDarkGray {
        UIColor *color = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];

        return [self getCGColor:color];
}

+(const CGFloat*) getCGColor: (UIColor*)color {
        CGColorRef colorref = [color CGColor];

        const CGFloat *components = CGColorGetComponents(colorref);

        return components;
}

此外,此代码在iOS 4.x和适用于iOS 5的模拟器中运行良好。唯一受损的地方是iOS 5设备。

3 个答案:

答案 0 :(得分:2)

使用[self setNeedsDisplay];

drawRect方法仅在init处调用,因此如果您的对象先前被引入并且您需要“刷新”它,它可以传递它。

答案 1 :(得分:0)

不要在代码中使用rect,而是尝试使用self.bounds。 Rect是需要刷新的区域,而不是视图的整个区域。

答案 2 :(得分:0)

iOS 5中的

drawRect:不在某些对象上,例如UINavigationBar。在这些情况下,您必须有条件地使用新的iOS 5样式方法。

This问题和this问题可能对您有用。