好吧,我有这个警告,我正在使用它,我希望它的背景是黑色而不是灰色的样子.我设法改变了标题和消息的文本颜色,但没有改变背景颜色.那么我想要的颜色.我已将其更改为绿色蓝色和白色,但不是黑色.当我尝试将其更改为黑色时,它会变为灰色.任何建议都会有所帮助并受到赞赏.我在
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,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 }
解决方法
试试这个
Swift2及以下
let subview :UIView = alert.view.subviews. first! as UIView let alertContentView = subview.subviews. first! as UIView alertContentView.backgroundColor = UIColor.blackColor()
目标-C
UIView *subView = alertController.view.subviews.firstObject; //firstObject UIView *alertContentView = subView.subviews.firstObject; //firstObject [alertContentView setBackgroundColor:[UIColor darkGrayColor]]; alertContentView.layer.cornerRadius = 5;
更新的答案迅速3及以上
let alert = UIAlertController(title: "validate",message: "Check the process",preferredStyle: .alert) let dismissAction = UIAlertAction(title: "Dismiss",style: .destructive,handler: nil) alert.addAction(dismissAction) self.present(alert,completion: nil) // change the background color let subview = (alert.view.subviews.first?.subviews.first?.subviews.first!)! as UIView subview.layer.cornerRadius = 1 subview.backgroundColor = UIColor(red: (195/255.0),green: (68/255.0),blue: (122/255.0),alpha: 1.0)
产量
苹果手机
iPad的