例如,在我当前的VC上,我没有显示状态栏,但是当我以模态方式显示另一个显示状态栏的VC时,当前的VC会执行一个看起来不稳定的移动动画。我怎么能不搞乱当前的VC,并且当它向上滑动时,状态栏中的模态呈现淡入淡出?
答案 0 :(得分:2)
var hideStatusBar = false
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
override var prefersStatusBarHidden: Bool {
return hideStatusBar
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
hideStatusBar = true
setNeedsStatusBarAppearanceUpdate()
}
一旦您的视图出现,此代码将平滑地滑动状态栏。您还可以通过替换以下来尝试不同的动画:
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
with:
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .fade
}
答案 1 :(得分:0)
使用self.prefersStatusBarHidden()取决于特定视图
override func prefersStatusBarHidden() -> Bool {
return true
}
答案 2 :(得分:0)
添加一个标志,确定状态栏是否隐藏
var statusBarHidden = false
覆盖prefersStatusBarHidden
override func prefersStatusBarHidden() -> Bool {
return statusBarHidden
}
添加实用程序功能以使用动画更新状态栏可见性
func setStatusBar(hidden: Bool) {
statusBarHidden = hidden
UIView.animate(withDuration: 0.25, animations: {
self.setNeedsStatusBarAppearanceUpdate()
}) { (success: Bool) in
print("status bar animated to hidden: \(statusBarHidden)")
}
}