Search code examples
iosobjective-cmpmovieplayercontrollerstatusbar

MPMoviePlayerViewController hides StatusBar and destroys Frame after watching


i created a MPMoviePlayerViewController in my UIView (not in my UIVIewController!) like this:

self.playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 70, 125, 100)];
                        [self.playButton setBackgroundImage:[UIImage imageNamed:@"video_play.png"] forState:UIControlStateNormal];
                        [self.playButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];


                        self.playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];

self.playerViewController.moviePlayer.fullscreen = NO;
self.playerViewController.view.frame = CGRectMake(0, 0, 320, 200);

[self.playerViewController.moviePlayer prepareToPlay];
self.playerViewController.moviePlayer.shouldAutoplay = NO;
self.playerViewController.view.backgroundColor = [UIColor yellowColor];
[self addSubview:self.playerViewController.view];
[self.playerViewController.view addSubview:self.playButton];

}

- (void)buttonPressed:(id)sender{
(NSLog(@"Click"));

[self.playerViewController.moviePlayer setFullscreen:YES animated:YES];
[self.playerViewController.moviePlayer play];
}

As you can see i added a Button on the videoView, beacause this part should only be a Preview and when the user clicks on the Button the MPMoviePlayerViewController should animate to fullscreen and start playing, and when the Video is finished it should go back to the Preview View. Everything works so far, but i have two Problems:

First Problem: Everytime i open the View my StatusBar becomes hidden and it looks like this:

enter image description here

So i set in my viewWillAppear and viewDidAppear of my UIViewController:

[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

This works, but now the statusbar gets hidden and immediately appears again, that looks ugly, any chance to solve this Problem?

Second Problem:

When i click on the Custom Button the Video gets fullscreen and everything works correct! But when i press the DONE Button of the Video everything goes back to the Preview Screen and it looks like this: The StatusBar is hidden, the navigationbar is also broken and there is a lot of black Space above the Video, whats the Problem here?

enter image description here


Solution

  • Ok i found a solution for this Problem, its a little bit hacky but i found no other solution. In my ViewController i do:

    - (void)viewDidLoad {
       [super viewDidLoad];
    
        float delay = 0.1;
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC),  dispatch_get_main_queue(), ^{
        [UIApplication sharedApplication].statusBarHidden = NO;
    });
    

    and for the case User taps back Button i do:

    - (void)viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
    
       [UIApplication sharedApplication].statusBarHidden = NO;
    }