iOS Navigation Bar behind status bar after closing full screen MPMoviePlayer

I had a problem with an iOS app I am creating. I have a view that has a UINavigationBar at the top and a table view  that displays a full screen video on didselectrowatindexpath. Once the video is playing if the device is rotated and the done button is pressed the video will disappear but the navigation bar has moved to the top of the screen displaying behind the status bar. This leaves a gap between the nav bar and the top of the table view.

Solution

I tried a number of things to fix it including setting the status bar to hidden and then not hidden once video had exited full screen. I also tried the same with the navigation bar.

The fix that actually worked in the end did follow these lines. I added observers to MPMoviePlayer notifications and hid the nav bar when the video entered full screen mode. Once the video exited full screen mode I showed the nav bar again.

 
Below are how you add the observers that are required. I placed them in the viewDidLoad method.

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didExitFullScreen:)
                                                 name:MPMoviePlayerDidExitFullscreenNotification
                                               object:nil];
 
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didEnterFullScreen:)
                                                 name:MPMoviePlayerDidEnterFullscreenNotification
                                               object:nil];

I then created the methods to hide and show the navigation bar. These methods can include any other UI updates you might need to do such as refreshing a table view.

-(void)didEnterFullScreen:(id)sender{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
 
-(void)didExitFullScreen:(id)sender{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
 }

After creating these the Navigation bar behaved as expected and no longer displayed behind the status bar.
There is still one more step to the process and that is removing the observers to the notifications. I added these to the dealloc method

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
One Response to iOS Navigation Bar behind status bar after closing full screen MPMoviePlayer
  1. Alberto Reply

    THANK YOU! 🙂

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.