Custom UIView using CALayers disappears after 180º rotation or navigation controller pop

Posted by Steve Madsen on Stack Overflow See other posts from Stack Overflow or by Steve Madsen
Published on 2010-06-14T07:08:21Z Indexed on 2010/06/14 7:12 UTC
Read the original article Hit count: 311

I have a created a custom UIView subclass that is exhibiting some strange behavior. It is a spinning wheel selector, and for performance reasons it is drawn entirely into two CALayer instances. The bottom layer is the wheel itself, which is rotated using setAffineTransform: according to touches. The top layer is eye candy.

drawRect: is fairly simple. If the control hasn't been drawn yet (or it's been invalidated), it calls a method that creates the images and assigns them to the layer contents property.

- (void) drawRect:(CGRect)rect
{
    if (imageLayer == nil)
    {
        [self drawIntoImageLayer];
    }

    [self updateWheelRotation];
}

When the view controller using this view first appears, everything is fine. There are two instances where the view completely disappears, however:

  1. If the device is rotated a full 180°.
  2. After a view controller is popped off the navigation stack and the view becomes visible again.

drawRect: is not called either time. Interestingly enough, it IS called after a 90° orientation change, and that causes the view to re-appear.

How can I ensure that a custom view using CALayers is redrawn properly in these situations?

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about uiview