iPhone X在主界面是UITabBarController的情況下,如果控制器的hidesBottomBarWhenPushed屬性設爲true,進行push操作之後會出現tabBar高度錯亂的現象,先變矮一截,然後返回的時候又彈回到正常高度,非常影響使用感受.如果再當前頁面點擊到可以旋轉的頁面,那麼這個問題會更加嚴重。
正常狀態:
頁面異常下:
import UIKit
class BaseUITabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.addObserver(self, forKeyPath: "frame", options: [.old, .new], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let tabBar = object as? UITabBar, keyPath == "frame" {
if let oldFrame = change?[.oldKey] as? CGRect, let newFrame = change?[.newKey] as? CGRect {
if oldFrame.size != newFrame.size {
if oldFrame.height > newFrame.height {
tabBar.frame = oldFrame
} else {
tabBar.frame = newFrame
}
}
}
}
}
deinit {
tabBar.removeObserver(self, forKeyPath: "frame")
}
}