Search code examples
iosuiviewuitableviewcore-animationcalayer

Autorotation issue with UITableViewCell and CALayer


I have a UITableViewCell subclass with backgroundView set to my own UIView object. This UIView object contains three CALayer layers. I implemented - (void)layoutSubviews where I update all my CALayer layers. The problem is autorotation.

When I rotate from landscape to portrait mode there's this cosmetic issue:

During the animation, all my CALayer layers are as narrow as in portrait mode.

It seems that this guy is right:

When layoutSubviews gets called during an orientation change, the view's bounds are already set to what they will be at the conclusion of the rotation.

Source: How to achieve smooth animation when using single-step rotation / How do I get the new frame size at the start of rotation?

So, where should I update my layers to achieve proper autorotation? The view is already rotating them, so I suppose there's no need to do any custom animations, just adjust the size. Right?


Solution

  • Figured it out:

    1. subclass CALayer + add sublayers into it
    2. implement resizing of sublayers in CALayer's - (void)layoutSublayers method
    3. subclass UIView + override + (Class)layerClass in it:

      + (Class)layerClass {
          return [SubclassedCALayer class];
      }
      
    4. set the subclassed UIView as backgroundView of UITableViewCell