Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 隐藏UIStatusBar而不删除为其分配的空间_Ios_Swift_Uistatusbar - Fatal编程技术网

Ios 隐藏UIStatusBar而不删除为其分配的空间

Ios 隐藏UIStatusBar而不删除为其分配的空间,ios,swift,uistatusbar,Ios,Swift,Uistatusbar,我有图片的例子来告诉你我想要什么和我现在拥有什么 首先,下面是我在Slack应用程序中尝试做的一个示例: 状态栏通常显示为: 但当你打开侧抽屉时,它就消失了: 我可以在我的应用程序中显示状态栏: 但当我隐藏它时,它也会隐藏框架,因此顶部的空间比以前小: 每当侧抽屉打开时,从顶部移除空间看起来很不可靠,但不隐藏状态栏也很糟糕,因为菜单的背景颜色不同。如何隐藏状态栏上的文本,同时保持其空间不变 我认为您需要以下内容(在Swift中,部署目标是9.0): 要隐藏它,请执行以下操作: U

我有图片的例子来告诉你我想要什么和我现在拥有什么

首先,下面是我在Slack应用程序中尝试做的一个示例:

状态栏通常显示为:

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

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

但当我隐藏它时,它也会隐藏框架,因此顶部的空间比以前小:


每当侧抽屉打开时,从顶部移除空间看起来很不可靠,但不隐藏状态栏也很糟糕,因为菜单的背景颜色不同。如何隐藏状态栏上的文本,同时保持其空间不变

我认为您需要以下内容(在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上创建松弛样式的侧栏视图(没有凹口)时,我遇到了状态栏收缩的问题,保持顶部间距的最佳方法是设置
附加安全区域insets.top=20
20pt
是状态栏高度的常数

演示图像:

(调整
其他安全区域插图
)是唯一对我有效的事情。
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)
}