Reputation: 572
I have an iPad application with a main view controller, which have a status bar and navigation bar on the top.
When I rotate this main view - everything is fine. But when I present a modal (full screen) view controller and rotate the device when its presented, this happens:
The presented view controller is rotated as expected.
When dismissed, the main view is still in the orientation which it was when presenting (wrong one), but the status bar is on top (like only the status bar was rotated).
After playing with the device a bit, the orientation changes to what it should, but now the status bar does not have it's 20px margin on top - it is now overlapping the navigation bar, which looks awful.
Any ideas on what caused this strange situation?
EDIT: not using UINavigationController prevents the statusBar overlapping the navigation bar issue, but the rotation issue still exists.
Upvotes: 4
Views: 1583
Reputation: 16714
From the UIViewController Class Reference:
When a rotation occurs for a visible view controller, the willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are called during the rotation. The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, the viewWillLayoutSubviews method is called when the view becomes visible. Your implementation of this method can call the statusBarOrientation method to determine the device orientation.
So, you can use the viewWillLayoutSubviews
method to rotate your presentingViewController according to the current statusBarOrientation. I haven't tested this code, but something like this should get you started:
- (void)viewWillLayoutSubviews {
[self willRotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:0];
[self willAnimateRotationToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:0];
}
Upvotes: 1