Ho avuto lo stesso problema. La mia soluzione è:
1.First di tutto accendere tutti gli orientamenti nel progetto Xcode:
![enter image description here](https://i.stack.imgur.com/bd4ho.jpg)
2.In AppDelegate.m aggiuntivo:
-(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;
}
In sopra il codice, ogni volta che il sistema richiede supportatoInterfaceOrientationsForWindow si controlla se currentController è quello in cui si ha inserisci UIWebView
con il player di YouTube incorporato e verifica anche se il video è in riproduzione ora (ovvero il video è in modalità schermo intero).
NOTA: Io uso UINavigationController
, in caso contrario, è necessario apportare alcune modifiche per ottenere corrente viewController.
3.In viewController dove ho messo UIWebView
per YouTube lettore incorporato (Nel mio caso è VideoViewController), nel suo file di intestazione aggiungere metodo:
+(BOOL)isVideoPlaying;
4 .in VideoViewController.m aggiungere variabile statica:
static BOOL _isVideoPlaying = NO;
5.In viewDidLoad aggiungere addObserver per le notifiche, al fine di sapere, quando il video ha iniziato a giocare e willExitPlaying:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillExitFullscreen:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
6.Inoltre, aggiungere i metodi di selezione di notifica:
-(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, aggiungere metodi VideoViewController.m:
+(BOOL)isVideoPlaying {
return _isVideoPlaying;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if(!isVideoPlaying) {
return toInterfaceOrientation != UIInterfaceOrientationLandscapeLeft && toInterfaceOrientation != UIInterfaceOrientationLandscapeRight;
}
return YES;
}
Tutti questi trucchi funziona bene per me, che supporta iOS 5 e più tardi.
Spero che funzioni per voi!
Grazie mille, funziona. Posso fornire fonti per chiunque, se necessario. – user2587950
@ user2587950 mi puoi inviare un progetto dimostrativo di questo? Ottengo errori? Non so come risolverlo. – karthikeyan