我有同样的问题。我的解决方案是:
1.首先在您的 xcode 项目中打开所有方向:
2.在AppDelegate.m添加:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSArray *stackViewControllers = self.navigationController.viewControllers;
UIViewController *rvc = [stackViewControllers objectAtIndex:stackViewControllers.count - 1];
if([rvc isKindOfClass:[VideoViewController class]])
{
id presentedViewController = [rvc presentedViewController];
NSString *viewControllerName = NSStringFromClass([presentedViewController class]);
if([viewControllerName isEqual:@"MPInlineVideoFullscreenViewController"] && [VideoViewController isVideoPlaying]) {
return UIInterfaceOrientationMaskAll;
}
}
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
在上面的代码中,每次系统询问supportedInterfaceOrientationsForWindow时,您都会检查当前viewController 是否是您UIWebView
嵌入youtube 播放器的位置,并检查视频是否正在播放(即视频处于全屏模式)。
注意:我使用UINavigationController
,如果不使用,则必须进行一些更改才能获取当前的viewController。
3.在我为youtube嵌入式播放器放置的viewController中(在我的情况下是VideoViewController),在它的头文件中添加方法:UIWebView
+(BOOL)isVideoPlaying;
4.在VideoViewController.m添加静态变量:
static BOOL _isVideoPlaying = NO;
5.在viewDidLoad添加 addObserver 用于通知,以便知道视频何时开始播放和willExitPlaying:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillExitFullscreen:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
6.另外,添加通知选择器方法:
-(void)playerStarted:(NSNotification *)notification{
_isVideoPlaying = YES;
}
-(void)playerWillExitFullscreen:(NSNotification *)notification {
_isVideoPlaying = NO;
if([AppUtils iOSVersion] < 6) //For iOS < 6.0, you must manually rotate viewController's view when fullscreen video playing is dismissed.
{
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
{
self.navigationController.view.userInteractionEnabled = NO;
[UIView animateWithDuration:0.5 animations:^{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
// rotate main view, in this sample the view of navigation controller is the root view in main window
[self.navigationController.view setTransform: CGAffineTransformMakeRotation(180 * M_PI * 0.5)];
// set size of view
[self.navigationController.view setFrame:CGRectMake(0, 0, 320, 960)];
} completion:^(BOOL finished) {
self.navigationController.view.userInteractionEnabled = YES;
}];
}
}
}
7.并且,在VideoViewController.m
中添加方法:
+(BOOL)isVideoPlaying {
return _isVideoPlaying;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if(!isVideoPlaying) {
return toInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && toInterfaceOrientation != UIInterfaceOrientationLandscapeRight;
}
return YES;
}
所有这些技巧对我来说都很好,支持 iOS 5 及更高版本。
希望这对你有用!