横向模式下的UINavigationBar高度不会完全显示

时间:2017-03-17 16:14:21

标签: ios swift uinavigationbar

我使用了UINavigationBar,当我改为横向模式时,条形图看起来像这样。

enter image description here

以纵向模式

enter image description here

1 个答案:

答案 0 :(得分:3)

尝试这种方式,navigation bar的代码不是navigation controller。可能您错过了设置约束或相互冲突

   override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
        super.viewWillTransition(to: size, with: coordinator)


           var frame: CGRect = (self.navigationController?.navigationBar.frame)!

                coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in

                    let orient = UIApplication.shared.statusBarOrientation

                    switch orient {
                    case .portrait:

                        frame.size.height = 44
                        frame.size.width = self.view.frame.width
                    default:

                        frame.size.height = 32
                        frame.size.width = self.view.frame.width
                    }

                }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
                    print("rotation completed")
                    self.navigationController?.navigationBar.frame = frame
                })

    }