iOS 5 UIView drawRect 覆盖在设备上不起作用
我正在准备在 iOS 5 GM 上发布我的 iPhone 应用程序,并遇到了 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 设备。
I'm preparing my iPhone app to publish on iOS 5 GM and came across a bug with UIView. When I override the drawRect method on a subclass, the Simulator shows the desired result but when I try to test on an actual device, the drawRect override doesn't have any effect at all.
I even placed a logging statement inside a drawRect override and confirmed that it is being called.
Has anyone else noticed this problem?
Here's a override code I'm using:
- (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));
}
Here's the code for generating color
+(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;
}
Again, this code works perfectly in iOS 4.x and in the Simulator for iOS 5. The only place that is broken is iOS 5 Device.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
[self setNeedsDisplay];
drawRect 方法仅在 init 时调用,因此如果您的对象之前已初始化并且您需要“刷新”它,则它可以传递该方法。
Use
[self setNeedsDisplay];
The drawRect method is called only at init, so it can pass that if your object was inited previously and you need to "refresh" it.
不要在代码中使用 rect,而是尝试使用 self.bounds。矩形是需要刷新的区域,而不是视图的整个区域。
Instead of using rect in your code, try using self.bounds. Rect is the area that needs refreshing, not the entire area of your view.
在 iOS 5 中,
drawRect:
不在某些对象上,例如UINavigationBar
。在这些情况下,您必须有条件地使用新的 iOS 5 样式方法。这个问题和这个问题可能对您有用。
in iOS 5
drawRect:
is not on some objects e.g.UINavigationBar
. You have to conditionally use the new iOS 5 styling methods under these circumstances.This question and this question might be useful for you.