完成按下电影播放器​​后还原为肖像?

我有一个MPMoviePlayer工作。 它被devise为在视图中显示邮票大小的电影作为子视图。 当手机旋转到横向时,进入全屏模式。 而当手机是肖像时,它会进入邮票肖像模式。

唯一的问题是,当我在横向模式下按Done时,它保持横向,邮票大小的电影,而不是踢回肖像..

这是我的一些代码:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { [moviePlayerController setFullscreen:YES animated:YES]; } else { [moviePlayerController setFullscreen:NO animated:YES]; } } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft; } 

按完成后,我怎样才能让它进入肖像模式?

@cannyboy …你只需要在你的APPDelegate.m中使用下面的方法,如果你的应用程序只能用于肖像模式

 - (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) { //NSLog(@"in if part"); return UIInterfaceOrientationMaskAllButUpsideDown; } else { //NSLog(@"in else part"); return UIInterfaceOrientationMaskPortrait; }} 

我是同样的问题,现在我告诉你我是如何解决这个问题的。 我不知道下面的方法是对还是错,但它工作正常。

  • 在视图中打开MPMoviePlayer而不是在新的viewController打开它。 我的意思是创build一个新的UIViewController来显示电影,并推动一些如何使用户不会明白,他们正在redirect到一个新的屏幕。
  • 禁用横向模式的父屏幕,并允许MovieViewController横向。
  • 当用户按完成button或closuresbutton只需pop viewController和前一个屏幕不支持横向模式,因此屏幕将自动显示在纵向模式。
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_moviePlayerWillExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; - (void)_moviePlayerWillExitFullscreen:(NSNotification *)notification { CGFloat ios = [[[UIDevice currentDevice] systemVersion] floatValue]; CGFloat min = 5.0; if (ios >= min) { if (self.interfaceOrientation != UIInterfaceOrientationPortrait) { if([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait]) { [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0]; [UIViewController attemptRotationToDeviceOrientation]; } } } } 

请注意,这只适用于ios 5.0及更高版本,并且您将收到setOrientation不受支持的警告,但它工作得很好

您可以尝试的一种方法是强制设备认为它处于纵向模式。 要做到这一点,请尝试使用:

 [[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait]; 

如果您不希望自己的设备在不播放电影的时候显示风景,还需要在上面显示的代码中添加一些逻辑,才能在电影播放时更改为风景。