我有同樣的問題。我的解決辦法是:
所有1.首先在你的Xcode項目打開所有方向:
2.In 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.In 的viewController,我,我已經把UIWebView
YouTube的嵌入式播放器(在我的情況下,它是VideoViewController),在它的頭文件中添加方法:
+(BOOL)isVideoPlaying;
4在VideoViewController.m添加靜態變量:
static BOOL _isVideoPlaying = NO;
5.In 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.And,添加方法VideoViewController.m:
+(BOOL)isVideoPlaying {
return _isVideoPlaying;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if(!isVideoPlaying) {
return toInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && toInterfaceOrientation != UIInterfaceOrientationLandscapeRight;
}
return YES;
}
所有這些技巧很適合我,支持iOS 5的然後。
希望它適合你!
非常感謝,它的工作原理。 我可以根據需要爲任何人提供資源。 – user2587950
@ user2587950你可以給我演示這個項目嗎?我得到錯誤?我不知道如何解決它。 – karthikeyan