【问题标题】:Change Status Bar Color in iOS 13? [duplicate]在 iOS 13 中更改状态栏颜色? [复制]
【发布时间】:2020-06-16 13:49:07
【问题描述】:

我一直在尝试更改状态栏颜色,但使用 key statusBar 我的应用程序崩溃了。 我猜 iOS 13 中没有键“statusBar”。

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

添加视图也不适合我。

【问题讨论】:

  • 请分享控制台日志并注明崩溃原因。
  • @DharmeshKheni 发生此崩溃:*** 断言失败 -[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:],*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“应用程序调用 - UIApplication 上的 statusBar 或 -statusBarWindow:此代码必须更改,因为不再有状态栏或状态栏窗口。在窗口场景中使用 statusBarManager 对象。'

标签: ios swift


【解决方案1】:

第一种方式

你可以检查 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
}

【讨论】:

    猜你喜欢
    • 2020-01-16
    • 2019-11-01
    • 2020-02-04
    • 2016-11-04
    • 2018-08-27
    • 2021-08-03
    • 2016-10-16
    • 2020-01-26
    相关资源
    最近更新 更多