-1

我在 App Store 上的应用程序崩溃了。

以下是我在崩溃日志中的内容。

我的项目中有基本视图控制器。我正在使用基本视图控制器扩展每个视图控制器。

在基本视图控制器中,在 viewDidAppear 中,我使用以下代码设置状态栏颜色。

static func adjustStatusBarToColor(colorAsString: String) {

    print("adjustStatusBarToColor===\(globalTopPadding)")

    if (globalTopPadding>=1) {
        if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
            let statusBar2: UIView = statusBar.subviews[0]
                let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")
                statusBar2.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
        }
    } else {
        if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
            let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")
            statusBar.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
        }
    }
}

知道为什么应用程序崩溃了吗?

在此处输入图像描述

4

1 回答 1

2

在调用之前检查元素是否响应特定的选择器,您将避免任何崩溃。

let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")

if statusBar2.responds(to: setForegroundColor_sel) {
    statusBar2.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
}

我相信这段代码会返回一些不响应颜色变化的视图:

let statusBar2: UIView = statusBar.subviews[0]
于 2019-07-11T12:36:49.633 回答