在iOS 13中更改状态栏颜色吗?

问题描述 投票:0回答:1

我一直在尝试更改状态栏颜色,但使用关键状态栏,但是我的应用崩溃了。我想键“ statusBar”在iOS 13中不可用。

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
  statusBar.backgroundColor = <Some Color>
}

添加视图也不适用于我。

ios swift background-color statusbar ios13
1个回答
0
投票

第一方式

您可以检查iOS版本并像这样添加自定义状态栏->

override func viewDidAppear(_ animated: Bool) {
    if #available(iOS 13, *) {
        let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
        statusBar.backgroundColor = .systemBackground
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    }
}

第二方式

您可以自定义UINavigationBarAppearance

if #available(iOS 13.0, *) {
    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithOpaqueBackground()
    navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    navBarAppearance.backgroundColor = <yourColor>
    navigationBar.standardAppearance = navBarAppearance
    navigationBar.scrollEdgeAppearance = navBarAppearance
}
© www.soinside.com 2019 - 2024. All rights reserved.