首页 文章

IOS 8 - 方向更改后,导航栏被状态栏遮挡

提问于
浏览
5

我'm using SWRevealViewController and my app is locked on portrait orientation. On one of my view controllers I'使用 YTPlayerView 启动了一个youtube播放器 . 在显示视频时,我会以编程方式将方向切换为横向,如下所示:

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

在退出视频时,像这样回到肖像:

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

一切正常,但引入了布局问题:导航栏Y没有被状态栏偏移,导致状态栏位于导航栏的顶部(参见屏幕) .

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

我在这里缺少什么:)?

1 回答

  • 0

    声明一个将缓存初始导航栏大小的var

    var navigationFrame = CGRect()
    

    然后,在viewDidLoad中保存默认值

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

    更改方向后调用此方法解决问题:

    self.navigationController?.navigationBar.frame = navigationFrame
    

    该解决方案的灵感来自@Shlomi

相关问题