Swift UIAlertController 背景色

Swift UIAlertController background color

我正在实施一个自定义警报,我在其中设置了背景颜色。当我在模拟器中压缩下面的代码时,它可以完美运行,但是当我在 iPhone 上测试时,自定义背景被如下所示的图案叠加。

let alertController = UIAlertController(title: "Opções", message: "Deseja realmente excluir esse serviço?", preferredStyle: .Alert)
alertController.setValue(NSAttributedString(string: "Opções", attributes: [NSFontAttributeName : UIFont.systemFontOfSize(17),NSForegroundColorAttributeName : UIColor.whiteColor()]), forKey: "attributedTitle")
alertController.setValue(NSAttributedString(string: "Deseja realmente excluir esse serviço?", attributes: [NSFontAttributeName : UIFont.systemFontOfSize(17),NSForegroundColorAttributeName : UIColor.whiteColor()]), forKey: "attributedMessage")

alertController.view.tintColor = UIColor.whiteColor()
let backView = alertController.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 5.0
backView?.backgroundColor = corAzul
backView?.alpha = 1.0
let imageView = UIImageView(frame: CGRectMake(10, 15, 30, 30))
imageView.image = UIImage(named: "load")
alertController.view.addSubview(imageView)

let Action2 = UIAlertAction(title: "Excluir", style: .Default) { (action) in
    Excluir()
}
let Action7 = UIAlertAction(title: "Cancelar", style: .Cancel) { (action) in
    Cancelar()
}
alertController.addAction(Action2)
alertController.addAction(Action7)
viewController.presentViewController(alertController, animated: true) {}

这是什么原因造成的?我该如何解决?

如果要更改背景颜色,请尝试更改alertController.view.subviews.first?.subviews.first?.subviews.last?.backgroundColor。但它仍然会有一个白色的封面。 UIAlertViewController 的视图层次结构并不那么简单。您不知道影响封面颜色的是图层还是视图。 您尝试更改 UIAlertController 的背景颜色。但我认为这不是一个好主意。我们不知道UIAlertController的iOS8和iOS9的区别。也许 alertController.view.subviews.first?.subviews.first?.subviews.last?.backgroundColor 在 iOS10 中有效,但在某些以前的版本中无效。你要改的API我们不开放,可以随意改。如果你想定制一个 UIAlertViewController,试着做一个全新的。

我找到了一个解决方案,我并不太自豪,因为它既不优雅也不与众不同,但它确实有效! 显然,当使用 "cont" 检查时,iOS 中的子视图数量在 10 之前结果为 1,然后 .last 或 .fist 都可以工作,但在 iOS 10 中,该数字与 1 不同,我无法确定它是否会保持不变,但我决定制作一个 "for" 来制作所有可能性。

alertController.view.tintColor = UIColor.whiteColor()
for i in 0 ..< alertController.view.subviews.count {
    alertController.view.subviews[i].backgroundColor = corAzul
    alertController.view.subviews[i].layer.cornerRadius = 5.0
    alertController.view.subviews[i].alpha = 1.0
    for ii in 0 ..< alertController.view.subviews[i].subviews.count {
        alertController.view.subviews[i].subviews[ii].backgroundColor = corAzul
        alertController.view.subviews[i].subviews[ii].layer.cornerRadius = 5.0
        alertController.view.subviews[i].subviews[ii].alpha = 1.0
        for iii in 0 ..< alertController.view.subviews[i].subviews[ii].subviews.count {
            alertController.view.subviews[i].subviews[ii].subviews[iii].backgroundColor = corAzul
            alertController.view.subviews[i].subviews[ii].subviews[iii].layer.cornerRadius = 5.0
            alertController.view.subviews[i].subviews[ii].subviews[iii].alpha = 1.0

        }
    }

}

我知道这不太好,但这是唯一有效的解决方案! 如果有人发现更复杂的东西,请随意贡献