我有图片示例,向您展示我想要的和我现在拥有的东西。
首先,这是我在Slack应用程序中尝试做的一个例子:
通常会显示状态栏:
但是当你打开侧抽屉时,它会消失:
我可以在我的应用中显示状态栏:
但是当我隐藏它时,它也隐藏了框架,因此顶部的空间比以前少:
每当侧抽屉打开时,从顶部移除空间看起来很不舒服,但由于菜单具有不同的背景颜色,因此也不会隐藏状态栏。如何保留状态栏上的文本,同时保留其中的空间?
答案 0 :(得分:2)
我认为您需要类似以下内容(在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);
})
我不确定你会遇到与我相同的问题,但是当我测试代码时,我原本没有#34; -0.00001"并且转换不顺利但是那个小的减法修正了它。不知道为什么。
答案 1 :(得分:1)
我的解决方案确实成功。
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
}
}
答案 2 :(得分:1)
我在 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)
}
答案 3 :(得分:0)
我无法在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)
})
}
}