Ios 允许MPMoviePlayerViewController在横向播放,同时保留显示视图控制器';s方向

Ios 允许MPMoviePlayerViewController在横向播放,同时保留显示视图控制器';s方向,ios,iphone,objective-c,uiviewcontroller,orientation,Ios,Iphone,Objective C,Uiviewcontroller,Orientation,我有一个MPMoviePlayerViewController(一个子类,而不是XCDYouTubeVideoPlayerViewController),我用以下代码演示它 LPVideo *video = [_videos objectAtIndex: indexPath.row]; XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController all

我有一个MPMoviePlayerViewController(一个子类,而不是XCDYouTubeVideoPlayerViewController),我用以下代码演示它

LPVideo *video = [_videos objectAtIndex: indexPath.row];
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier: video.code];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
我的问题是,虽然整个应用程序都锁定在纵向模式下,但我仍然希望用户在横向模式下播放视频,所以我将其放在了AppDelegate中

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
    return UIInterfaceOrientationMaskPortrait;
}
}
这在允许用户以纵向方向观看视频方面效果良好;但是,如果视频播放器在纵向模式下关闭,则显示的viewcontroller也会切换到纵向,这是我不希望发生的事情

我尝试过各种方法,比如在我的主UINavigationController中实现
支持的界面定向
应该自动旋转
,但它甚至没有阻止横向定向

我知道这是可能的,因为我见过应用程序这样做,但我不知道怎么做。我见过一些解决方案,包括倾听方向的变化,并将转换应用于播放器视图,但这似乎不必要地复杂

我还尝试通过
viewwilldisease:animated
在返回时“强制”旋转,但调用代码时没有帮助

这两个我都试过了

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

[UIViewController尝试旋转到设备定向]


两者都一事无成。

问题不是那么清楚,如果你想在所有方向播放视频,那么就用“UIInterfaceOrientationMaskAll”而不是“UIInterfaceOrientationMaskAllButUpsideDown”。
如果不起作用,则检查您正在使用的库是否有任何库,并更改方向。

我通过检查
IsBeingDisposed
解决了此问题:

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]] && ![[self.window.rootViewController presentedViewController] isBeingDismissed]) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
但是,嘿,我用的是斯威夫特:

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
}

就像我说的,在第一段代码之后,视频播放器可以很好地旋转;问题是在关闭横向播放器时,显示电影播放器的ViewController也在横向中,这不是我支持的方向-我的问题是如何防止这种行为。我遇到了同样的问题,这是因为我使用了库[即ViewDeck]正在改变方向。所以你能检查一下吗。似乎不是这样;我刚刚尝试在一个只使用UINavigationController的项目中使用相同的代码,我得到了相同的结果将“self.window”替换为“window”,它就会工作。啊,太好了,这正是我需要的!非常感谢你有解决办法吗?我也面临同样的问题。我的应用程序处于纵向模式,只有MPMoviePlayerVersionController应该在横向模式下播放视频。使用SupportedInterfaceOrientionsForWindow,我可以在lanscape中播放视频,但在关闭playercontroller后,应用程序仍然处于横向模式,这不是我支持的方向。提前谢谢!检查下面的答案,它对我有效!(您必须在AppDelegate中实现我的代码,但不要使用self.window,只需使用window即可)谢谢!我很高兴能帮上忙,但如果不是Shubham和他们的评论,我永远不会去,所以他们应该得到所有的信任。上面的答案是好的,但在解散时,它允许屏幕处于风景中。这迫使一切回到肖像。谢谢