So, I have a project where I need to force an orientation change when the user clicks a button. I demonstrated the problem with an example application on github .
@interface DefaultViewController () { UIInterfaceOrientation _preferredOrientation; }
Some rotation processing bits
- (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return _preferredOrientation; }
And switch
- (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 works fine, when you just press the toggle button, it changes orientation as expected. But when I start to change the actual orientation of the device, a problem arises.
Steps to reproduce the problem:
- Open application in portrait orientation
- Press the button to change the orientation to landscape orientation (saving the actual device in the portrait)
- Press the button again to make the rotation return to the portrait (keeping the image in the portrait)
- Turn the actual device into landscape without pressing a button
The result is that the view does not rotate to landscape, but in the status bar.

Any help would be greatly appreciated!
source share