ios - 如何在iPhone X中设置带有背景图片的黑色状态栏?

标签 ios swift statusbar

应用程序以黄色图像为背景,状态栏设置为隐藏,在除 iPhone X 之外的其他设备上运行良好。

状态栏保持白色文本颜色。

enter image description here

我尝试添加以下代码,仍然失败:

override var preferredStatusBarStyle: UIStatusBarStyle{
  return .default      
}

还有其他建议吗?

谢谢。

最佳答案

方法一:

您必须将此值添加到 plist:“查看基于 Controller 的状态栏外观”并将其设置为“NO”。

之后将其添加到AppDelegate

   var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        if #available(iOS 11.0, *) {
            if (window?.safeAreaInsets.top)! > CGFloat(0.0) || window?.safeAreaInsets != .zero {
                print("iPhone X")
                application.isStatusBarHidden = true
                //or UIApplication.shared.isStatusBarHidden = true
            }
            else {
                print("Not iPhone X")
                application.isStatusBarHidden = true
            }
        }
        return true
    }

方法2:查看基于 Controller 的状态栏外观”并将其设置为“”。 默认情况下。

在 iOS11+ 中, setStatusBarHiddenisStatusBarHidden 已被弃用, [prefersStatusBarHidden][2] 从 iOS7+ 开始可用,我们可以通过 ViewController 进行状态栏可见性设置 -

struct StatusBarInfo {
    static var isToHiddenStatus = false
  }
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        if #available(iOS 11.0, *) {
            if (window?.safeAreaInsets.top)! > CGFloat(0.0) || window?.safeAreaInsets != .zero {
                print("iPhone X")
                StatusBarInfo.isToHiddenStatus = true
            }
            else {
                StatusBarInfo.isToHiddenStatus = true
                print("Not iPhone X")
            }
        }
        return true
    }

在 ViewController.Swift 中

override var prefersStatusBarHidden: Bool {
        return StatusBarInfo.isToHiddenStatus
    }

关于ios - 如何在iPhone X中设置带有背景图片的黑色状态栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48165043/

相关文章:

iphone - 核心位置 : How do I wake my app up when iPhone turned on after significant location change?

ios - 一个应用程序为不同的客户提供多种外观? (苹果规则)

swift - SpriteKit - 如何生成相似的角色?

android - 如何将渐变应用于android中的状态栏?

ios - setStatusBarStyle :animated: deprecated

android - 当我打开黑暗模式时,黑暗图标...我该如何解决...?

ios - UIApplicationShortcutItem 不启动

ios - 按位置确定 Sprite 的名称

swift - 阿拉伯文本剥离器/解析器

ios - cellForRow At IndexPath 执行一次太少?