2014-09-22 153 views
5

我的应用程序包含在横向和纵向模式下播放视频的功能。视频也可以是YouTube一切工作正常,直到iOS 7,但现在youtube视频不能在横向模式下工作在iOS 8Youtube视频不能在横向模式下播放iOS 8

我的代码:

- (NSUInteger)application:(UIApplication *)applicationsupportedInterfaceOrientationsForWindow:(UIWindow *)window { 


    if ([[window.rootViewController presentedViewController] 
    isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")])  { 

     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } else { 

     if ([[window.rootViewController presentedViewController] 
     isKindOfClass:[UINavigationController class]]) { 

      // look for it inside UINavigationController 
      UINavigationController *nc = (UINavigationController *)[window.rootViewController presentedViewController]; 

      // is at the top? 
      if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) { 
      return UIInterfaceOrientationMaskAllButUpsideDown; 

      // or it's presented from the top? 
      } else if ([[nc.topViewController presentedViewController] 
        isKindOfClass:[MPMoviePlayerViewController class]]) { 
       return UIInterfaceOrientationMaskAllButUpsideDown; 
      } 
     } 
    } 

    return UIInterfaceOrientationMaskPortrait; 
} 

一切工作正常,直到iOS的7,但停止在iOS 8 工作任何帮助表示赞赏

回答

14

那么它有时傻傻地回答自己的问题,但这很好帮助其他面临同样问题的人。

在iOS 8中,而不是检查MPInlineVideoFullscreenViewController,我们需要检查AVFullScreenViewController。所以下面是所有iOS版本的完整方法,例如iOS 8或更少。

- (NSUInteger)application:(UIApplication *)applicationsupportedInterfaceOrientationsForWindow:(UIWindow *)window { 

    if ([[window.rootViewController presentedViewController] 
    isKindOfClass:[MPMoviePlayerViewController class]] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"MPInlineVideoFullscreenViewController")] || [[window.rootViewController presentedViewController] isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) { 

     return UIInterfaceOrientationMaskAllButUpsideDown; 
    }else { 

     if ([[window.rootViewController presentedViewController] 
     isKindOfClass:[UINavigationController class]]) { 

      // look for it inside UINavigationController 
      UINavigationController *nc = (UINavigationController *)[window.rootViewController presentedViewController]; 

      // is at the top? 
      if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) { 
       return UIInterfaceOrientationMaskAllButUpsideDown; 

       // or it's presented from the top? 
      } else if ([[nc.topViewController presentedViewController] 
        isKindOfClass:[MPMoviePlayerViewController class]]) { 
       return UIInterfaceOrientationMaskAllButUpsideDown; 
      } 
     } 
    } 

    return UIInterfaceOrientationMaskPortrait; 
} 

更新: 作品中的iOS 9以及

+4

upvoted cuz回答你自己的问题并不傻。未答复的帖子是愚蠢的。 – overeasy 2014-09-25 05:13:03

+0

感谢您的答案,我有麻烦使它在iOS7上工作 - 细节太大,不能评论,提出了一个新问题http://stackoverflow.com/questions/26443485/fullscreen-video-screen-is-not -mpmovieplayerviewcontroller-on-ios7-device – Boaz 2014-10-18 19:07:59

+0

谢谢!视频视图处于活动状态时,旋转按预期工作。我遇到了一个问题,在应用程序处于横向模式时点击完成按钮后,应用程序返回到纵向模式,但导航栏无法正确调整大小。导航栏的高度比应该小。 – 2014-12-10 23:00:23

0

我无法检测到AVFullScreenViewController我在iOS上的8金主应用程序,但发现AVPlayerView的伎俩。

的UIViewController + VideoAutorotate.h

#import <UIKit/UIKit.h> 

@interface UIViewController (VideoAutorotate) 

@end 

的UIViewController + VideoAutorotate.m

#import "UIViewController+VideoAutorotate.h" 

BOOL testAnyViewRecursively(UIView *view, BOOL (^test)(UIView *view)) { 
    if (test(view)) { 
     return YES; 
    } else { 
     for (UIView *subview in view.subviews) { 
      if (testAnyViewRecursively(subview, test)) { 
       return YES; 
      } 
     } 
    } 
    return NO; 
} 

@implementation UIViewController (VideoAutorotate) 

-(BOOL)shouldAutorotate 
{ 
    if (UI_PAD) { 
     return YES; 
    } else { 
     // iOS 6: MPInlineVideoFullscreenViewController in iOS 6 doesn't seem to override this method to return YES. 
     if ([NSStringFromClass([self class]) isEqual:@"MPInlineVideoFullscreenViewController"]) { 
      return YES; 
     } 
     // iOS 8: 
     return testAnyViewRecursively(self.view, ^BOOL(UIView *view) { 
      return [NSStringFromClass([view class]) isEqual:@"AVPlayerView"]; 
     }); 
    } 
} 
+0

您无法检测到AVFullScreenViewController,因为您是内省视图而不是视图控制器。 – overeasy 2014-09-26 05:16:02

+0

当我试图找到'AVFullScreenViewController'时,在我的代码中使用'[self class]'像iOS 6的行。 – hiroshi 2014-09-26 06:35:15

+0

@Rajat感谢您收集一些拼写和语法,但我的意思是GM是“Golden Master”而不是GSM(全球移动通信系统)。 – hiroshi 2014-10-28 12:44:20

5

我在我的应用程序,这也打破了在iOS8上类似的代码。

我只是想发布我的这个修复版本,以防万一它帮助任何人。

主要的区别是,我只是检查最顶级的控制器。

我认为这比嵌套的条件试图找出什么样的vc呈现另一个vc更有意义。

不管怎么说,我在我的应用程序代表了这一点,它在8

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    id presentedViewController = [self topMostController]; 

    if ([self vcIsVideoPlayer:presentedViewController]) { 
     return UIInterfaceOrientationMaskAll; 
    } else { 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

- (UIViewController*) topMostController { 
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; 

    while (topController.presentedViewController) { 
     topController = topController.presentedViewController; 
    } 

    return topController; 
} 

- (BOOL) vcIsVideoPlayer:(UIViewController *)vc { 
    NSString *className = vc ? NSStringFromClass([vc class]) : nil; 
    return (
      [className isEqualToString:@"MPInlineVideoFullscreenViewController"] || 
      [className isEqualToString:@"MPMoviePlayerViewController"] || 
      [className isEqualToString:@"AVFullScreenViewController"] 
      ); 
} 
+1

完美的作品,但当视频停止播放时,底层视图控制器呈现在横向上! – entropid 2014-10-25 14:22:37

+0

工程像魅力。谢谢 – devxoul 2015-12-04 16:14:51

3

更新伟大的工作: 一件事,如果你发现你的状态栏打破你回来之后添加从横向视频到控制器的操作是在viewWillLayoutSubviews中将状态栏设置为不隐藏为假。

override func viewWillLayoutSubviews() { 
    super.viewWillLayoutSubviews() 
    UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .None) 
} 

对于那些在斯威夫特中,一些额外的笔记。此方法(application:supportedInterfaceOrientationsForWindow)应位于AppDelegate类中,或者您已设置为@UIApplicationMain的任何位置。为了获得MPMoviePlayerViewController课程,您必须记住import MoviePlayer

其次,UIInterfaceOrientationMask值不与自身雨燕版本委托的兼容,所以你需要访问rawValue和改造所产生的UintInt。这是需要帮助的Swift解决方案。

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow) -> Int { 
    var orientation = UIInterfaceOrientationMask.Portrait 

    if let presentedController = window.rootViewController?.presentedViewController { 

     //check for the controllers 
     if presentedController is MPMoviePlayerViewController || 
      presentedController.isKindOfClass(NSClassFromString("AVFullScreenViewController").self) || 
      presentedController.isKindOfClass(NSClassFromString("MPInlineVideoFullscreenViewController").self) { 
      orientation = .AllButUpsideDown 
     } 
     //otherwise, we may be inside a Nav. 
     //safely get the nav controller otherwise ignore this block 
     else if let navController = presentedController as? UINavigationController { 

      if navController.topViewController is MPMoviePlayerViewController || 
       navController.topViewController.isKindOfClass(NSClassFromString("AVFullScreenViewController").self) || 
       navController.topViewController.isKindOfClass(NSClassFromString("MPInlineVideoFullscreenViewController").self) { 
       orientation = .AllButUpsideDown 
      } 
     } 


    } 

    return Int(orientation.rawValue) 
} 
1

这是Swift在iOS7和iOS8上测试的解决方案。您必须将此方法添加到您的AppDelegate类。

AppDelegate.swift

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { 

    var topController = UIApplication.sharedApplication().keyWindow?.rootViewController 

    if (topController != nil) { 
     while ((topController!.presentedViewController) != nil) { 
      topController = topController!.presentedViewController; 
     } 

     if (topController != nil && (topController!.className == "AVFullScreenViewController" || topController!.className == "MPFullScreenTransitionViewController")) { 
      return Int(UIInterfaceOrientationMask.All.rawValue); 
     } 

    } 

    return Int(UIInterfaceOrientationMask.Portrait.rawValue); 
} 
+0

我得到这个错误:方法'application(_:supportedInterfaceOrientationsForWindow :)'提供的Objective-C方法'application:supportedInterfaceOrientationsForWindow:'与协议'UIApplicationDelegate'中的可选需求方法'application(_:supportedInterfaceOrientationsForWindow :)'冲突“ 。还有什么你必须做的来实现这个代码? – tomDev 2016-02-02 18:33:56

0

这里是夫特版本3适用于iOS 10.1。我在这里修改了Anthony Persaud的答案。要检查if presentedController.isKind(of: MPMoviePlayerViewController.self),您需要顶部有import MediaPlayer

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 

    if let presentedController = window?.rootViewController?.presentedViewController, 
     let avFullScreen = NSClassFromString("AVFullScreenViewController").self, 
     let mpInlineVideoFullscreen = NSClassFromString("MPInlineVideoFullscreenViewController").self { 

     if presentedController.isKind(of: MPMoviePlayerViewController.self) || 
      presentedController.isKind(of: avFullScreen) || 
      presentedController.isKind(of: mpInlineVideoFullscreen) { 
      return UIInterfaceOrientationMask.allButUpsideDown 
     } 
    } 

    return UIInterfaceOrientationMask.portrait 

} 
相关问题