navigation bar under status bar after video playback in landscape mode

Viewed 6230

The problem:

Navigation bar is under status bar after playing video in landscape mode.

The application:

  • iOS9 only.
  • only supports portrait mode.
  • the view controller have a web view on it, web view will open a youtube link
  • the view controller is embedded in a navigation controller

Setups to reproduce:

  1. Play a video in a webView,
  2. Put device into landscape mode.
  3. Dismiss video play in landscape mode, app goes back to portrait mode
  4. Navigation bar is in wrong position

Screenshots:

  1. When app opens

when app opens

  1. Play video and put the device in landscape

enter image description here

  1. The problem:

enter image description here

4 Answers

I tried @Makalele's answer but didn't not quite working (or it might but got blocked due to other test code). After some test and try, I end up with something simpler than that.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(setNeedsStatusBarAppearanceUpdate)
                   name:UIDeviceOrientationDidChangeNotification
                 object:nil];
    [center addObserver:self
               selector:@selector(setNeedsStatusBarAppearanceUpdate)
                   name:UIWindowDidBecomeHiddenNotification
                 object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (BOOL)prefersStatusBarHidden {
    return UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
}

A few things to notice

  1. You can call setNeedsStatusBarAppearanceUpdate directly using selector.
  2. Added a removeObserver call when view disappears.
  3. The return value of prefersStatusBarHidden must be changed from time-to-time.

So, in the view controller that contains the YouTube view, you will see the status bar disappears before entering YouTube fullscreen. It will returns when the YouTube playing is completed (via UIWindowDidBecomeHiddenNotification event).

Just in case that this event doesn't trigger, the other event: UIDeviceOrientationDidChangeNotification, will still trigger whenever user rotates the screen (even the orientation is locked).

So, @Makalele's solution has double route to trigger status bar.

I found that I don't need UIDevice:setValue:forKey: but your mileage may vary.

Credit to @Makalele and @Aaron.

Related