Stack Overflow上有很多关于此问题的答案,但似乎都没有。你如何使UIAlertViewController的背景色真正清晰?
我现在有:
let errorAlert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let subview = errorAlert.view.subviews.first! as UIView
let alertContentView = subview.subviews.first! as UIView
alertContentView.backgroundColor = UIColor.clearColor()
errorAlert.view.backgroundColor = UIColor.clearColor()
showViewController(errorAlert, sender: self)
但结果是图像上有一种白色的透明背景... 无论如何都要删除这个有色背景?
答案 0 :(得分:1)
您需要访问UIAlertController的超级视图
alertView.view.superview?.backgroundColor = UIColor.clearColor()
答案 1 :(得分:0)
为了实现这一点,需要将所有子视图的颜色设置为UIColor.clear
。此外,所有UIVisualEffectView
子视图都需要删除。这可以通过使用递归函数(Swift 4)来实现:
func clearBackgroundColor(of view: UIView) {
if let effectsView = view as? UIVisualEffectView {
effectsView.removeFromSuperview()
return
}
view.backgroundColor = .clear
view.subviews.forEach { (subview) in
clearBackground(of: subview)
}
}
在创建UIAlertController
实例后立即调用此命令:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
clearBackgroundColor(of: alert.view)
如果您现在想要更改警报的外观:
alert.view.layer.backgroundColor = UIColor.red.withAlphaComponent(0.6).cgColor
alert.view.layer.cornerRadius = 5