I have a project where the rotation events stop firing after removing/adding a lot of view/viewControllers. It works for a while and then at random the new view added to the window never gets shouldAutorotateToInterfaceOrientation: called.
The status bar fails to rotate as well but the view/view controller functions normal (minus rotation). Also there is only one view / viewController added to the window at a time. I’m assuming a old viewController is registered as key but everything added to window is a subclass of view controller and none of the subclasses are showing up in instrument allocations except the active one.
Is there a way on the app delegate or window to find which view controller is key?
Is there any other reason why the shouldAutorotateToInterfaceOrientation: could be failing to get called?
Each view controller is implementing:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)
interfaceOrientation duration: (NSTimeInterval) duration {
//manual rotation code
}
Here is the code I’m using in the application delegate to change the root view controller and the displayed view.
[UIView transitionWithView:new.view
duration:0.3
options:UIViewAnimationOptionCurveEaseOut
animations:^{
//add new view
[self.window addSubview:new.view];
//play transition animation for old and new views
new.view.transform = CGAffineTransformConcat(new.view.transform, newEnd);
old.view.transform = CGAffineTransformConcat(old.view.transform, oldEnd);
//if old view is a "modal style" then keep it in front on new view
if([old conformsToProtocol:@protocol(PreloadableModalView)]) {
[self.window bringSubviewToFront:old.view];
}
}
completion:^(BOOL finished){
//remove old view
[old removeSubviews];
[old.view removeFromSuperview];
}];
It looks like the root of the problem was that removing the viewController that the window was sending the rotation event during the transition caused the window to sometimes not register the new viewcontroller as the new root.
I just have one UIView (with its own UIViewController) added to the Window. During the animation I was removing the root view controller and adding a new one to the screen. The fix was to switch view controllers before the animation.
I created a screenshot of the current view
removed the old view controller
set the new view controller as the rootviewcontroller and add the view to the window
ran the transition animation
oncomplete i delete the image
this is the main parts of the code: