There still seems to be some debate about how best to accomplish this task, so I thought I'd share my (working) approach. Add the following code in your UIViewController
implementation:
- (void) viewWillAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
-(BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
For this example, you will also need to set your allowed device orientations to 'Landscape Left' in your project settings (or directly in info.plist
). Just change the specific orientation you want to force if you want something other than LandscapeLeft.
The key for me was the attemptRotationToDeviceOrientation
call in viewWillAppear
- without that the view would not properly rotate without physically rotating the device.