6

My app's supported interface orientations are Portrait and Upside down. However, when a video is played, I want it to play full screen landscape. Currently with this code, it only plays portrait, even when the device is rotated:

[player.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
player.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self presentMoviePlayerViewControllerAnimated:player];

How can I force it into landscape mode?

soleil
  • 12,133
  • 33
  • 112
  • 183
  • For iOS6, this answer is better : http://stackoverflow.com/questions/12577879/shouldautorotatetointerfaceorientation-is-not-working-in-ios-6 – fpauer May 29 '13 at 22:53

1 Answers1

11

Here is how I do it:
In the project file, make sure you are supporting the Landscape Orientations supported interface orientations
Now in all of your ViewControllers that should still be Portrait Only, add this code

//ios6
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

//ios4 and ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

I have put in both the iOS5 and iOS6 calls so that my code will run on both.

When your MPMoviePlayerController view becomes fullscreen, it will be a new ViewController layered on top of everything else. So, it will be allowed to rotate according to the Supported Interface Orientations of the Project. It will not see where you forced the other ViewControllers into Portrait.

Walter
  • 5,867
  • 2
  • 30
  • 43
  • 1
    thanks, I will resort to this if I have to, but I'm hoping there's a way to do it without having to put that orientation code in every view controller (there are a lot). – soleil Oct 23 '12 at 00:48
  • What I do is create a UIViewController that just has these methods in it. Then I make all of my UIViewControllers that need to be portrait just inherit from this instead of inheriting from UIViewController. In the `@interface` line of the `.h` file I put the name of MyPortraitViewController in the place of UIViewController. – Walter Oct 23 '12 at 12:00
  • @soleil, to my understanding, this is the only way to do it, unfortunately... the reason why is in the Apple docs on `UIViewController` under **supportedInterfaceOrientations** section: `The system intersects the view controller’s supported orientations with the app's supported orientations (as determined by the Info.plist file or the app delegate's application:supportedInterfaceOrientationsForWindow: method) to determine whether to rotate.` – JRG-Developer Jan 11 '13 at 00:33
  • 2
    @Walter with first 3 methods included in superclass of all controllers (iOS6) it still auto-rotates in simulator. Am I missing something? – esp May 23 '13 at 10:45
  • if you only support iOS 7, shouldAutorotate should do the job – Marc Aug 11 '14 at 15:46