I am trying to update my willAnimateRotationToInterfaceOrientation code to use the new viewWillTransitionToSize as a part of IOS 9.
In my app, I have two totally separate views for landscape and portrait (they are laid out differently, in a different pattern).
During rotation the self.view of the viewcontroller is assigned to the correct view (landscape or portrait). In IOS 9 using viewWillTransitionToSize, this revealed a timing race condition between the rotation animation and the assignment of the self.view.
The assignment of self.view = portraitView or self.view = landscapeView could happen either before the rotation, or after the rotation. It appears that because a view assignment is not an "animatable" property that it is executed at any time during the animateWithDuration window.
How do I force the view assignment to happen before the rotation animation?
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
UIInterfaceOrientation oldOrientation = [[UIApplication sharedApplication] statusBarOrientation];
UIInterfaceOrientation orientation = [Utilities orientationByTransforming:[coordinator targetTransform] fromOrientation:oldOrientation];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
[UIView animateWithDuration:[context transitionDuration] animations:^{
if (UIInterfaceOrientationIsLandscape(orientation)) {
self.view = self.landscapeView;
}
else{
self.view = self.portraitView;
}
} completion:^(BOOL finished){
//no completion logic for animateWithDuration
}];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
//no completion logic for animateAlongsideTransition
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}