我在App Store上的应用程序崩溃了。
以下是崩溃日志中的内容。
我的项目中有基本 View Controller 。我用基本 View Controller 扩展了每个 View Controller 。
在基本 View Controller 的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))
}
}
}
知道为什么应用程序崩溃了吗?
最佳答案
在调用元素之前,检查元素是否响应特定的选择器,您将避免崩溃。
let setForegroundColor_sel: Selector = NSSelectorFromString("setForegroundColor:")
if statusBar2.responds(to: setForegroundColor_sel) {
statusBar2.perform(setForegroundColor_sel, with: UIColor(hexString: colorAsString))
}
我相信这段代码返回的 View 不会响应颜色的变化:
let statusBar2: UIView = statusBar.subviews[0]
关于ios - UIResponder didNotRecognizeSelector:应用程序因此错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56989240/