答案 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
})
}