Ios – navigation bar under status bar after video playback in landscape mode

iosiphoneobjective cswift

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

Best Answer

Swift 3

In the presenting view controller, override the prefersStatusBarHidden property to only hide the status bar if it's in landscape.

override var prefersStatusBarHidden: Bool {
    return UIApplication.shared.statusBarOrientation.isLandscape
}

Then add an observer for when the device is rotated.

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(videoDidRotate), name: .UIDeviceOrientationDidChange, object: nil)
}

In the observer's method, call setNeedsStatusBarAppearanceUpdate:

func videoDidRotate() {
    self.setNeedsStatusBarAppearanceUpdate()
}

That should do it.

Related Topic