Extremely strange behavior of navigationBar and MPMoviePlayerController. Error in iOS or my mistake?

I have an MPMoviePlayerController object that plays full-screen video in portrait or landscape orientation. If I rotate the orientation during video playback and perform the rotation within a few seconds after the video starts playing, and the video status bar is displayed when the video ends, my navigation bar is perfect. But if I wait until the video status bar disappears a few seconds before the video plays, and then rotate the orientation when the video ends, my navigationBar partially hides behind the status bar, for example, is pressed up.

Have you ever seen something like this?

I can easily recreate this error. I created a new Single View application and just added a button and a navigation bar. If I rotate the orientation while playing a video, click to turn on the full screen mode and the video status bar is still visible when the video ends, everything is fine. But, if I wait to rotate after the disappearance of the video status bar, when I rotate and the video ends, the navigation bar is in the status bar. See Image:

IPhone image

Here is the simple code that I use:

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

This is where I am now with the suggestions below. I must have something wrong, because, unfortunately, I still have the same problem.

Here is the onPlayerWillExitFullScreen method

 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]; 
+6
source share
3 answers

Ok, so I found this damn same error in my entire application, first in UIWebView and then in MPMoviePlayerController, I decided that I put 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]; } 

Erroneous errors, complex corrections.

+5
source

If you listen to the MPMoviePlayerWillExitFullscreenNotification notification, you can force a redraw of the main views as follows. The "window" referenced is the main object of your UIWindow application.

When MPMoviePlayerController switched to full-screen mode, it actually creates a separate instance of UIWindow to represent the video. By catching the notification when it moves backward, this code will provide returned views in order to rebuild correctly.

Admittedly, this is not an elegant solution, but it works every time.

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

To listen for this notification, you need to do something like this, where self.playerController is your MPMoviePlayerController object.

Do not forget to stop listening to this notification as soon as you release the player!

  // 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]; 
+5
source
 - (void) moviePlayerWillExitFullScreen:(id)sender { [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:NO]; } 

Guys are trying this ... This works for me. I tried many other ways, and only this one worked.

+3
source

Source: https://habr.com/ru/post/902901/


All Articles