Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow MPMoviePlayerViewController to play in landscape while preserving presenting view controller's orientation

I have a MPMoviePlayerViewController (a subclass rather, XCDYouTubeVideoPlayerViewController) that I'm presenting with the following code

LPVideo *video = [_videos objectAtIndex: indexPath.row];
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier: video.code];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];

My issue is that while the entire app is locked in Portrait mode I still want the user to play videos in landscape, so I've put this in my AppDelegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
    return UIInterfaceOrientationMaskPortrait;
}
}

This works fine in allowing the user to watch the video in portrait orientation; however if the video player is dismissed in portrait mode the viewcontroller that presented has also switched to portrait, which is something I don't want happening.

I have tried all sorts of things, like implementing supportedInterfaceOrientations and shouldAutorotate in my main UINavigationController, but it doesn't even prevent landscape orientation.

I know this is possible because I've seen apps doing this, but I can't figure out how. I've seen some solutions that involve listening to orientation changes and applying transformations to the player view but it seems needlessly complicated.

I also tried "forcing" rotation upon return via viewWillAppear:animated but while the code is called it doesn't help.

I've tried both this

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];

And this

[UIViewController attemptRotationToDeviceOrientation];

Both achieve nothing.

like image 531
Haiku Oezu Avatar asked Dec 20 '22 16:12

Haiku Oezu


1 Answers

I fixed the issue by checking isBeingDismissed:

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] && ![[self.window.rootViewController presentedViewController] isBeingDismissed]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }

but hey, I'm using Swift:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> UIInterfaceOrientationMask {
    //If the video is being presented, let the user change orientation, otherwise don't.
    if let presentedViewController = window.rootViewController?.presentedViewController? {
        if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
            return .AllButUpsideDown
        }
    }
    return .Portrait
}
like image 54
GBF_Gabriel Avatar answered May 11 '23 10:05

GBF_Gabriel