Alex Stone
Alex Stone

Reputation: 47348

iOS8, how to know when UIView autorotation is complete?

I'm working with a storyboard project using size classes and autolayout. However, there are a couple instances in code where I'm adding "old school" menus and components on screen. These components are drawn correctly until the view autorotates.

I'm trying to fix the autorotation issues for controls added to UIView programmatically in iOS8. How do I determine when a UIView autorotation has completed and the view has new bounds?

There's this method which is called before rotation is completed, and view still has old size, and subviews cannot properly redraw themselves. I do not see anything along the lines of didTransition

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{

//this does not seem to work - uses old size instead of new one
    [introductionView setNeedsDisplay];

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

Upvotes: 1

Views: 826

Answers (1)

matt
matt

Reputation: 535202

The method is called viewWillTransitionToSize:withTransitionCoordinator:. The second parameter is the transition coordinator! It tells you when the rotation is over.

Here's a typical structure from my own code (in Swift, but I'm sure you can translate mentally):

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    coordinator.animateAlongsideTransition({
            _ in
            // ...
        }, completion: {
            _ in
            // ... now the transition is over! ...
        })
}

Upvotes: 3

Related Questions