iOS 5 UIView drawRect override not working on device

北城余情 提交于 2019-12-07 03:52:18

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7699617/ios-5-uiview-drawrect-override-not-working-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!