3

I have videos throughout my app. Some using MPMoviePlayerController, others in a UIWebView with YouTube. I want my app to be totally portrait. However, I want to give the user the option to flip to landscape when there's a video (not force, but optional).

I've been searching the web for an answer, but I haven't found anything yet.

Thanks for your help!

jordangrogan
  • 251
  • 3
  • 11

5 Answers5

7

I had the same issue and fixed it by adding this in my app delegate, basically allowing Landscape orientation only on subclasses of MPMoviePlayerViewController:

#import <MediaPlayer/MediaPlayer.h>

@implementation UIViewController (orientationFix)

-(NSUInteger) supportedInterfaceOrientations
{
    if ([[self class] isSubclassOfClass:[MPMoviePlayerViewController class]]) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([[self class] isSubclassOfClass:[MPMoviePlayerViewController class]]) {
        return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
    }
    return UIInterfaceOrientationPortrait;
}

@end

@implementation MyAppDelegate
.
.
.
@end
Anas
  • 866
  • 1
  • 13
  • 23
  • In my case, I use YTPlayerView inside a normal View Controller so this method won't work, because it will allow the whole view controller to be landscaped, instead of only when the video is playing. – Van Du Tran Aug 18 '15 at 20:37
  • @VanDuTran hi! Did you find a solution for that please? I'm also using YTPlayerView. And also did the YTPlayer sound works for you in a real device? – Ne AS Nov 16 '16 at 00:48
0

either subclass it and -

(BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation { //return landscape here } for ios6: shouldAutorotate return no

or

use this:

MPMoviewPlayerController fullscreen playback rotation with underlying UIViewController with portrait mode only (rotation disallowed)

Community
  • 1
  • 1
Jatin
  • 1,668
  • 2
  • 16
  • 23
0

Add These Lines in AppDelegate.

-(BOOL)shouldAutorotate { return NO; }

-(NSUInteger)supportedInterfaceOrientations
{
    //LandScapeMode:- UIInterfaceOrientationMaskLandscape;
    //PortraitMode:- 
    return UIInterfaceOrientationMaskPortrait 
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait
}
Rameshios
  • 1
  • 4
0

Add this in app delegate.

-(BOOL)shouldAutorotate
    {
        return NO; 
    }

-(NSUInteger)supportedInterfaceOrientations
{
    //LandScapeMode:- UIInterfaceOrientationMaskLandscape;
    //PortraitMode:- 
    return UIInterfaceOrientationMaskPortrait 
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait
}
Rameshios
  • 1
  • 4
0
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theurl];
self.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.view.transform = CGAffineTransformConcat(self.moviePlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[self.moviePlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:self.moviePlayer.view];
[self.moviePlayer play];
Pratik Gujarati
  • 302
  • 2
  • 3