好的,所以我有这个正在使用的警报,我希望它的背景是黑色而不是灰色。我设法更改了标题和消息的文本颜色,但没有更改背景颜色。好到我想要的颜色。我已将其更改为绿色蓝色和白色,但不是黑色。当我尝试将其更改为黑色时,它会变成灰色。任何建议都会有所帮助,我们将不胜感激。我在这里试过了How to change the background color of the UIAlertController?这就是我到达现在位置的方式。
这是我现在要做的:
func showAlert(title:String, message:String) {
//Set up for the title color
let attributedString = NSAttributedString(string: title, attributes: [
NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here,
NSForegroundColorAttributeName : UIColor.whiteColor()
])
//Set up for the Message Color
let attributedString2 = NSAttributedString(string: message, attributes: [
NSFontAttributeName : UIFont.systemFontOfSize(15), //your font here,
NSForegroundColorAttributeName : UIColor.whiteColor()
])
let alert = UIAlertController(title: title,message: message, preferredStyle: .Alert)
alert.setValue(attributedString, forKey: "attributedTitle")
alert.setValue(attributedString2, forKey: "attributedMessage")
//alert.view.tintColor = UIColor.whiteColor()
let dismissAction = UIAlertAction(title: "Dismiss", style: .Destructive, handler: nil)
alert.addAction(dismissAction)
self.presentViewController(alert, animated: true, completion: nil)
//set the color of the Alert
let subview = alert.view.subviews.first! as UIView
let alertContentView = subview.subviews.first! as UIView
alertContentView.backgroundColor = UIColor.blackColor()
//alertContentView.backgroundColor = UIColor.greenColor()
//Changes is to a grey color :(
/*
alertContentView.backgroundColor = UIColor(
red: 0,
green: 0,
blue: 0,
alpha: 1.0)
//Also another Grey Color Not batman black
*/
//alertContentView.backgroundColor = UIColor.blueColor()
//turns into a purple
}
最佳答案
swift 4.1:
这是最适合我的方式:
func testAlert(){
let alert = UIAlertController(title: "Let's See ..",message: "It Works!", preferredStyle: .alert)
let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)
// Accessing alert view backgroundColor :
alert.view.subviews.first?.subviews.first?.subviews.first?.backgroundColor = UIColor.green
// Accessing buttons tintcolor :
alert.view.tintColor = UIColor.white
alert.addAction(dismissAction)
present(alert, animated: true, completion: nil)
}
关于ios - 更改 UIAlertcontroller 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293656/