navigationBar和MPMoviePlayerController极其怪异的行为。 在iOS中的错误或我的错误?

我有一个MPMoviePlayerController对象,以纵向或横向播放全屏video。 如果我在播放video的同时旋转方向,并在video开始播放后的几秒钟内进行旋转,并且video状态栏可见,则在video结束时,我的导航栏非常完美。 但是,如果等到video状态栏消失几秒钟后进入video播放,然后旋转方向,当video结束时,我的navigationBar栏部分隐藏在状态栏的后面,就像向上推。

你有没有见过这样的事情?

我能够轻松地重新创build这个bug。 我创build了一个新的单一视图应用程序,并简单地添加一个button和一个导航栏。 如果我在播放video的同时旋转方向,点击启用全屏,video状态栏仍然可见,当video结束时,一切正常。 但是,如果我在video状态栏消失之后等待旋转,则当我旋转并且video结束时,导航栏将位于状态栏下方。 见图:

iPhone图像

这是我正在使用的简单的代码:

 - (void) playMovie { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"movie" ofType: @"mov"]]; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackDidFinishNotification object: moviePlayer]; moviePlayer.controlStyle = MPMovieControlStyleDefault; moviePlayer.shouldAutoplay = YES; [self.view addSubview: moviePlayer.view]; [moviePlayer setFullscreen: YES animated: YES]; - (void) moviePlayBackDidFinish: (NSNotification *) notification MPMoviePlayerController *player = [notification object]; [[NSNotificationCenter defaultCenter] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: player]; if ([player respondsToSelector: @selector(setFullscreen:animated:)]) { [player.view removeFromSuperview]; } 

以下是我现在正在使用的build议。 我一定有什么错,因为不幸的是我仍然有同样的问题。

这里是onPlayerWillExitFullScreen的方法

 UIView *view = [[[UIApplication sharedApplication] delegate].window.subviews lastObject]; if (view) { [view removeFromSuperview]; [[[UIApplication sharedApplication] delegate].window addSubview:view]; } MPMoviePlayerController *player = [aNotification object]; [[NSNotificationCenter defaultCenter] removeObserver: self name: MPMoviePlayerWillExitFullscreenNotification object: player]; 

这里是我目前的playMovie方法:

  NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"movie" ofType: @"mov"]]; moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: url]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackDidFinishNotification object: moviePlayer]; [[NSNotificationCenter defaultCenter]addObserver: self selector: @selector(onPlayerWillExitFullScreen:) name: MPMoviePlayerWillExitFullscreenNotification object: self.moviePlayer]; moviePlayer.controlStyle = MPMovieControlStyleDefault; moviePlayer.shouldAutoplay = YES; [self.view addSubview: moviePlayer.view]; [moviePlayer setFullscreen: YES animated: YES]; 

好吧,所以我发现这个怪异的bug遍布我的应用程序首先在UIWebView然后在MPMoviePlayerController,我解决了这个代码放在我的视图控制器。

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self.navigationController setNavigationBarHidden:YES animated:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } 

棘手的错误,棘手的修复。

如果您侦听MPMoviePlayerWillExitFullscreenNotification通知,您可以强制您的主要视图正确重绘,如下所示。 引用的“窗口”是您的应用程序的主UIWindow对象。

当MPMoviePlayerController切换到全屏时,它实际上创build一个单独的UIWindow实例来呈现video。 通过在通知过渡后捕获通知,此代码将确保您切换回来的视图正确重新alignment。

诚然,这不是一个优雅的解决scheme,但它每次都有效。

 UIView *view = [window.subviews lastObject]; if (view) { [view removeFromSuperview]; [window addSubview:view]; } 

要听这个通知,你需要做这样的事情,其中​​self.playerController是你的MPMoviePlayerController对象。

记住,一旦你释放玩家,就停止听这个通知!

  // Determine the default notification centre NSNotificationCenter *centre = [NSNotificationCenter defaultCenter]; // Listen for interesting movie player notifications [centre addObserver: self selector: @selector(onPlayerWillExitFullScreen:) name: MPMoviePlayerWillExitFullscreenNotification object: self.playerController]; 
 - (void) moviePlayerWillExitFullScreen:(id)sender { [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:NO]; } 

你试试这个…它适用于我。 我尝试了很多其他的方法,只有这个工作。