我有一个 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;
}
}
这在允许用户以纵向观看视频方面效果很好;但是,如果在纵向模式下关闭视频播放器,则显示的 View Controller 也会切换为纵向模式,这是我不希望发生的事情。
我尝试了各种方法,比如在我的主 UINavigationController 中实现 supportedInterfaceOrientations
和 shouldAutorotate
,但它甚至没有阻止横向方向。
我知道这是可能的,因为我见过应用这样做,但我不知道如何做。我见过一些涉及监听方向变化和将转换应用到播放器 View 的解决方案,但它似乎不必要地复杂。
我还尝试通过 viewWillAppear:animated
在返回时“强制”旋转,但是在调用代码时它没有帮助。
这两个我都试过了
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];
还有这个
[UIViewController attemptRotationToDeviceOrientation];
两者都一事无成。
最佳答案
我通过检查 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
}
关于ios - 允许 MPMoviePlayerViewController 横向播放,同时保留呈现 View Controller 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22920079/