隐藏 UIStatusBar 而不删除为其分配的 space

Hide UIStatusBar without removing the space allocated for it

我有图片示例来向您展示我想要什么以及我现在拥有什么。

首先,这是我正在尝试做的一个示例,来自 Slack 应用程序:

状态栏正常显示:

但是当你打开侧抽屉时,它就消失了:

我可以在我的应用程序中显示状态栏:

但是当我隐藏它的时候,它也隐藏了框架,所以顶部比以前少了space:

每当侧边抽屉打开时从顶部移除 space 看起来很奇怪,但由于菜单具有不同的背景颜色,因此不隐藏状态栏看起来也很糟糕。如何在保持 space 仍然存在的同时隐藏状态栏上的文本?

我认为您需要如下内容(在 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”,并且过渡并不顺利,但是小减法修复它。不知道为什么。

我无法获得在 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)
        })
    }
}

这个解决方案我确实成功了。

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
    }
}

我在 iPhone 8(没有缺口)上创建松弛样式侧边栏视图时遇到状态栏收缩问题,保持顶部间距的最佳方法是设置 additionalSafeAreaInsets.top = 2020pt是状态栏高度的常量。

演示图片: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)
}