MPMoviePlayerController关闭后顶部状态栏消失

9
我在我的iPhone应用程序中遇到了一个有趣的小问题。我有一个视图和一个表格视图,每个单元格被点击时,会全屏播放视频,然后当你按下“完成”按钮时,视频就停止并返回到表格视图。唯一的问题是,当你在前2或3秒内按下“完成”按钮时,当视图返回到表格视图时,位于屏幕顶部告诉时间和电池强度等信息的栏不再存在,只剩下一个白色的空白。但如果你在几秒钟后按下完成按钮,则返回表格视图时一切都绝对正常!我完全不知道为什么会发生这种情况,我在互联网上找到的唯一一件事是这篇文章,其中有一个人几乎和我一样的问题:http://www.iphonedevsdk.com/forum/iphone-sdk-development/53020-disappearing-status-bar.html。这让我尝试使用:
[UIApplication sharedApplication].statusBarHidden = NO;

然而,这也没有任何进展。
当他们点击视频时执行的代码:
NSString *path = [[NSBundle mainBundle] pathForResource:currentTitle ofType:@"m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
movieController = [[MPMoviePlayerController alloc] initWithContentURL:url];
[movieController setControlStyle:MPMovieControlStyleFullscreen];
[movieController setFullscreen:YES];
movieController.view.frame = self.view.bounds;
[self.view addSubview:movieController.view];

[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

当视频播放完毕或用户点击“完成”时执行的代码如下:

NSLog(@"movieController moviePlayBackDidFinish");
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

[movieController setFullscreen:NO animated:NO];
[movieController.view removeFromSuperview];

[movieController release];

LiveEventsView *liveEventsView = [[LiveEventsView alloc] initWithNibName:@"LiveEventsView" bundle:nil];
UIView *currentView = self.view;
UIView *theWindow = [currentView superview];
UIView *newView = liveEventsView.view;
newView.frame = CGRectMake(0, 20, 320, 460);
[currentView removeFromSuperview];
[theWindow addSubview:newView];
[UIApplication sharedApplication].statusBarHidden = NO;

如果有人能够解决这个问题,我将非常感激,因为这让我非常沮丧!谢谢,马特。
3个回答

6
也许视频视图消失时的动画会与状态栏的动画发生时间上的冲突。 尝试将statusBarHidden = NO的调用延迟几秒钟。
NSInteger delay = 3;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[UIApplication sharedApplication].statusBarHidden = NO;
});

谢谢,这确实有效,但似乎成功延迟的最短时间是1秒,这有点太长了。如果这是唯一的解决办法,那就可以接受,但最好的情况是在视图加载后立即显示,而不是延迟1秒。 - Matthew Hallatt

6
您可以将延迟设置为浮点数。因此,它应该是:
float delay = 0.1;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        [UIApplication sharedApplication].statusBarHidden = NO;
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
    });

我曾经遇到过同样的问题,通过对richerd的代码进行微调解决了它。在我看来,0.1秒是可以接受的。我还不得不改变状态栏的样式,因为它返回了一个BlackTranslucent条形样式,而原始样式是BlackOpaque。但现在工作得很好。


3
我发现在给出的解决方案中,内容经常消失在状态栏下面。这种方法修复了这个问题。
注册MPMoviePlayerWillExitFullscreenNotification通知。
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerWillExitFullscreen:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:self.moviePlayer];

然后重置状态栏的可见性,从主窗口中删除并重新添加rootViewController,这将确保视图的边界再次正确。

- (void)moviePlayerWillExitFullscreen:(NSNotification *)notification {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

    id rootViewController = appDelegate.window.rootViewController;
    appDelegate.window.rootViewController = nil;
    appDelegate.window.rootViewController = rootViewController;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接