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

我正在准备我的iPhone应用程序发布iOS 5 GM,并遇到与UIView的错误。 当我重载子类的drawRect方法时,模拟器显示所需的结果,但是当我尝试在实际设备上进行testing时,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设备。

使用[self setNeedsDisplay];

drawRect方法仅在init时被调用,所以如果你的对象先前被inited,那么它可以传递,你需要“刷新”它。

而不是在代码中使用rect,请尝试使用self.bounds。 Rect是需要清爽的区域,而不是整个区域。

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

这个问题和这个问题可能对你有用。