我有图片示例,向您展示我想要的和我现在拥有的东西。
首先,这是一个我正在尝试做的例子,来自Slack应用程序:
通常会显示状态栏:
但是当你打开侧抽屉时,它会消失:
我可以在我的应用中显示状态栏:
但是当我隐藏它时,它也隐藏了框架,所以顶部的空间比以前少:
每当侧抽屉打开时,从顶部移除空间看起来很不可思议,但由于菜单具有不同的背景颜色,因此也不会隐藏状态栏。如何保留状态栏上的文本,同时保留其中的空间?
我想你想要以下内容(在Swift中,Deploy target是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”并且转换不顺畅但是那个小的减法修正了它。不知道为什么。
我确实成功解决了这个问题。
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
}
}
我无法在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)
})
}
}