gpt4 book ai didi

ios - 如何在 iOS 13 中调暗状态栏

转载 作者:行者123 更新时间:2023-11-29 05:26:53 27 4
gpt4 key购买 nike

我一直在开发一个 iOS 应用程序,在 iOS 13 之前,它能够在弹出窗口出现时使状态栏“变暗”。这只需添加一个带有边界的 View 作为状态栏并应用即可完成0.5 alpha:

lazy var fadeStatusBar: UIView = {
let fadeStatusBar = UIView(frame: UIApplication.shared.statusBarView?.frame ?? UIApplication.shared.statusBarFrame)
fadeStatusBar.backgroundColor = UIColor.black
fadeStatusBar.alpha = 0
fadeStatusBar.translatesAutoresizingMaskIntoConstraints = false
return fadeStatusBar
}()

其中 statusBarView 是 UIApplication 的扩展:

extension UIApplication {
var statusBarView: UIView? {
if #available(iOS 13.0, *) {
let tag = 38482458385
if let statusBar = self.keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag

self.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}
}

然后我可以将 fadeStatusBar alpha 属性设置为 0.5。然而,这在 iOS 13 中不再起作用。我只是想知道它是否仍然可以完成,因为我读到 another thread native iOS 应用程序也删除了此功能:

The status bar is rendered by a system process (out of the app process) on iOS 13 so there is no way for an app to change this. (Source: speaking to UIKit engineers at the WWDC 2019 labs.)

You can see this in the Apple Books app, which used to dim the status bar in its dark mode on iOS 12, but this does not happen on iOS 13.

不过,我的问题与上面提到的问题不同,因为问题是编辑状态栏本身的 alpha,而我很乐意使用覆盖 View 。任何能达到预期结果的建议将不胜感激。

最佳答案

您可以添加 View 并使其变暗,然后将其作为 subview 添加到状态栏,如下所示:

// Dim status bar
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
let wFrame = window?.windowScene?.statusBarManager?.statusBarFrame
let statusBarView = UIView(frame: wFrame!)
statusBarView.restorationIdentifier = "statusBarView"
statusBarView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
window?.addSubview(statusBarView)

如果您想将其恢复回来,您可以执行以下操作:

// Revert status bar back to normal
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
for subview in window!.subviews {
if subview.restorationIdentifier == "statusBarView" {
subview.removeFromSuperview()
}
}

关于ios - 如何在 iOS 13 中调暗状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58074865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com