So, I've got a project where I need to force an orientation change when a user presses a button. I've created a sample app on github to demonstrate the issue.
@interface DefaultViewController () {
UIInterfaceOrientation _preferredOrientation;
}
Some rotation handling bits
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return _preferredOrientation;
}
And a toggle
- (IBAction)toggleButtonPressed:(id)sender {
_preferredOrientation = UIInterfaceOrientationIsPortrait(_preferredOrientation)
? UIInterfaceOrientationLandscapeRight
: UIInterfaceOrientationPortrait;
[self forceOrientationChange];
}
- (void)forceOrientationChange
{
UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:NO completion:nil];
[UIView animateWithDuration:.3 animations:^{
[vc dismissViewControllerAnimated:NO completion:nil];
} completion:nil];
}
So, this seems to work fine when you just press the toggle button, it changes the orientation as expected. But when I start changing the actual orientation of the device, there is an issue.
Steps to reproduce the issue:
- Open app in portrait orientation
- Press the button to force the orientation change to landscape (Keeping the actual device in portrait)
- Press the button again to force the rotation back to portrait (Still keeping the device in portrait)
- Rotate the actual device to landscape without pressing the button
The result is that the view does not rotate to landscape, but the status bar does.
Any help would be greatly appreciated!