IOS 5 UIView drawRect override not working on device

I am preparing my iPhone application to publish it in iOS 5 GM and stumbled upon an error with UIView. When I override the drawRect method in a subclass, the simulator shows the desired result, but when I try to test on the device itself, overriding drawRect has no effect whatsoever.

I even placed the registration operator in the override of drawRect and confirmed that it was being called.

Has anyone else noticed this problem?

The override code is used here:

- (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 is 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 fine in iOS 4.x and in Simulator for iOS 5. The only place that is broken is the iOS 5 device.

+6
source share
3 answers

Use [self setNeedsDisplay];

The drawRect method is only called in init, so it can pass this if your object was previously entered, and you need to β€œupdate” it.

+2
source

Instead of using rect in your code, try using self.bounds. Rect is an area that needs to be updated, not the entire area of ​​your view.

0
source

in iOS 5 drawRect: not on some objects, for example. UINavigationBar . In these circumstances, you need to conditionally use the new iOS 5 styling methods.

This question and this may be useful to you.

0
source

Source: https://habr.com/ru/post/898898/


All Articles