iOS - Allow Landscape mode on videos only

2019-03-31 05:44发布

问题:

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!

回答1:

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


回答2:

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)



回答3:

Add These Lines in AppDelegate.

-(BOOL)shouldAutorotate { return NO; }

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait
}


回答4:

Add this in app delegate.

-(BOOL)shouldAutorotate
    {
        return NO; 
    }

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //LandScapeMode:- UIInterfaceOrientationLandscapeRight;
   // ProtraitMode:-
   return UIInterfaceOrientationPortrait
}


回答5:

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];