答案 0 :(得分:2)
这真是一个有趣的问题,有时带我去弄明白。
如果你注意到这样的viewcontroller中的app store没有状态栏,这意味着绿色栏根本不应该显示。这是第一个提示。
您可以通过UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
根据第一点,这意味着状态栏没有被隐藏,但实际上它的statusBarWindow.frame.origin.y
刚刚向上移动。
请不要单独处理iPhone x
请确保状态栏未隐藏
请注意,这不是唯一正确的方法,现在您拥有视图本身,您可以更改原点或大小,甚至尝试获取此视图中的内容并隐藏它们或更改其帧等。
这是一个如何做到的例子。
class ViewController: UIViewController {
var isHidden:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.5) { () -> Void in
let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
statusBarWindow.frame = CGRect(x: 0, y: -20, width: statusBarWindow.frame.size.width, height: 40.0)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var prefersStatusBarHidden: Bool{
return isHidden
}
}
还附上了一张包含此代码结果的图片,希望这个答案对你也有用。