Allow MPMoviePlayerViewController to play in the landscape while maintaining the orientation of the view view controller

I have an MPMoviePlayerViewController (a subclass, rather, XCDYouTubeVideoPlayerViewController), which I present with the following code

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

My problem is that while the entire application is locked in portrait mode, I still want the user to play the video in landscape orientation, so I added this to my AppDelegate

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

This works great, allowing the user to watch video in portrait orientation; however, if the video player is disabled in portrait mode, the presented view manager also switched to portrait, which I do not want to do.

, supportedInterfaceOrientations shouldAutorotate UINavigationController, .

, , , , , . , , .

"" viewWillAppear:animated, .

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

[UIViewController attemptRotationToDeviceOrientation];

.

+4
2

, , "UIInterfaceOrientationMaskAll" "UIInterfaceOrientationMaskAllButUpsideDown". , , - , , .

+3

, . , 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;
        }
    }

, 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
}
+8

Source: https://habr.com/ru/post/1535421/


All Articles