Search code examples
iphoneiosuinavigationbarmpmovieplayercontroller

Extremely weird behavior of navigationBar and MPMoviePlayerController. Bug in iOS or my error?


I have a MPMoviePlayerController object that plays a video fullscreen in either portrait or landscape. If I rotate orientation while the video is playing and do the rotation within a few seconds after the video starts playing and the video status bar is visible, when the video ends my navigation bar is perfect. But if I wait until the video status bar disappears a few seconds into the video playing and then rotate orientation, when the video ends my navigationBar is partially hidden behind the status bar, like pushed up.

Have you ever seen something like this?

I am able to recreate this bug easily. I created a new Single View App and simply added a button and a navigation bar. If I rotate orientation while video is playing, tap to enable fullscreen and the video status bar is still visible, when the video finishes, all is good. But, if I wait to rotate after the video status bar disappears, when I rotate and the video finishes, the navigationBar is under the status bar. See image:

iPhone Image

Here is the simple code I am using:

- (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];
}

Here is where I am currently at with the suggestions given below. I must have something wrong because unfortunately I still have the same problem.

Here is the method 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];    

and here is my current playMovie method:

 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];

Solution

  • Ok, so I found this freaking same bug all over my app first in a UIWebView then in a MPMoviePlayerController, I solved this placing this code in my view controller.

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

    Tricky bugs, tricky fixes.