【问题标题】:Hide UIStatusBar without removing the space allocated for it隐藏 UIStatusBar 而不删除为其分配的空间
【发布时间】:2015-12-29 20:45:30
【问题描述】:
我有图片示例向您展示我想要什么以及我现在拥有什么。
首先,这是我正在尝试做的一个示例,来自 Slack 应用程序:
状态栏正常显示:
但是当你打开侧抽屉时,它就消失了:
我可以在我的应用中显示状态栏:
但是当我隐藏它时,它也隐藏了框架,所以顶部的空间比以前少了:
当侧边抽屉打开时从顶部移除空间看起来很奇怪,但不隐藏状态栏也看起来很糟糕,因为菜单具有不同的背景颜色。如何隐藏状态栏上的文本,同时保留它的空间?
【问题讨论】:
-
This(调整additionalSafeAreaInsets)是唯一对我有用的东西。
标签:
ios
swift
uistatusbar
【解决方案1】:
我认为您需要类似以下的内容(在 Swift 中,部署目标是 9.0):
隐藏它:
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .Fade)
let appFrame:CGRect = UIScreen.mainScreen().applicationFrame
UIView.animateWithDuration(0.3, animations: {
self.navigationController?.navigationBar.frame = self.navigationController!.navigationBar.bounds
self.view.window!.frame = CGRectMake(0, 0, appFrame.size.width, appFrame.size.height);
})
再次显示:
let appFrame:CGRect = UIScreen.mainScreen().applicationFrame
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .Fade)
UIView.animateWithDuration(0.3, animations: {
self.navigationController?.navigationBar.frame = self.navigationController!.navigationBar.bounds
self.view.window!.frame = CGRectMake(0, 0, appFrame.size.width, appFrame.size.height-0.00001);
})
我不确定您是否会遇到与我相同的问题,但是当我测试代码时,我最初没有那个“-0.00001”,并且转换并不平滑,但是那个小减法解决了它。不知道为什么。
【解决方案2】:
我无法在 Swift 3 中获得在 iOS 10 上工作的公认答案。所以这就是我想出的:
class ViewController: UIViewController {
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.3, animations: {
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + 20)
})
}
}
【解决方案3】:
这个解决方案我确实成功了。
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector("statusBar")) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
func hideStatusBar() {
statusBarView?.frame.origin.y = -200
statusBarView?.isHidden = true
}
func showStatusBar() {
statusBarView?.frame.origin.y = 0
statusBarView?.isHidden = false
}
}
【解决方案4】:
我在 iPhone 8(没有缺口)上创建松弛样式侧边栏视图时遇到状态栏缩小,保持顶部间距的最佳方法是设置 additionalSafeAreaInsets.top = 20,20pt 是状态栏的常量高度。
演示图片:https://i.stack.imgur.com/MmeXw.gif
func hasNotch() -> Bool {
return self.view.safeAreaInsets.bottom > 20
}
if show {
UIView.animate(withDuration: 0.5, delay: 0,usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
self.centerView.view.frame.origin.x = self.centerView.view.frame.width - self.menuWidth
self.menuView.view.frame.origin.x = 0
// IMPORTANT: The code below fix status bar shrink when device has no notch.
if !self.hasNotch() {
self.additionalSafeAreaInsets.top = 20
}
}, completion: nil)
} else {
UIView.animate(withDuration: 0.5, delay: 0,usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
self.centerView.view.frame.origin.x = 0
self.menuView.view.frame.origin.x = -self.menuWidth
// IMPORTANT: The code below fix status bar shrink when device has no notch.
if !self.hasNotch() {
self.additionalSafeAreaInsets.top = 0
}
}, completion: nil)
}