IOS 8 - After orientation change, Navigation bar is obscured by the status bar

1k views Asked by At

I'm using SWRevealViewController and my app is locked on portrait orientation. On one of my view controllers I'm launching a youtube player using the YTPlayerView. When displaying the video I switch the orientation to landscape programmatically like so:

let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

and when exiting the video, back to portrait like so:

let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

All works fine, but introduce a layout issue: the Navigation bar Y is not offsetted by the status bar, resulting the status bar to be on top of the Navigation bar (see screens).

Before orientation change

Before orientation

Playing video on landscape orientation

Youtube player

Navigation bar is being obscured by the status bar

enter image description here

What am I missing here :) ?

1

There are 1 answers

0
Marwen Doukh On

Declare a var that will cache initial navigation bar size

  var navigationFrame = CGRect()

Then, in viewDidLoad save the default value

override func viewDidLoad() {
if let navFrame = self.navigationController?.navigationBar.frame {
      navigationFrame = navFrame
    }
  }

After changing orientation call this method to solve to problem:

self.navigationController?.navigationBar.frame = navigationFrame

This solution is inspired from @Shlomi