Search code examples
iosanimationorientationdrawrect

How to call drawRect after an orientation rotation animation is completed


I am trying to use drawRect to draw a rectangle in the space below a collectionView contained in another view (self in the code below) using the frames of both views to define the rectangle.

This code works fine until the device orientation changes. Everything I have tried calls drawRect before the animated rotation begins. This means the collectionView.frame and self.frame have not yet been adjusted by autoLayout and the resulting rectangle is wrong.

CGRect frame = self.collectionViwOutlet.frame;
CGFloat x = frame.origin.x;
CGFloat y = frame.origin.y + frame.size.height + 8;
CGFloat w = frame.size.width;
CGFloat h = self.frame.origin.y + self.frame.size.height - y - 8;
CGRect drawableRect = CGRectMake(x, y, w, h);

Any suggestions would be appreciated.


Solution

  • On your custom UIView override there layoutSubviews, which will be called for you by the system only when needed and when all the variables (orientation, size, etc.)

    PS.: Consider it: https://stackoverflow.com/a/19505582/846780