更改UIAlertcontroller背景颜色

好,所以我有这个警报,我正在使用,我想它的背景是黑色的,而不是灰色的。 我设法改变标题和消息的文本的颜色,但不是背景颜色。 那么我想要的颜色。 我已经把它改成绿色的蓝色和白色,但不是黑色的。 当我尝试将其改为黑色时,它变成灰色。 任何build议将有助于和赞赏。 我在这里试过如何更改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 } 

尝试这个

Swift3

  let subView = alertController.view.subviews.first! Var alertContentView = subView.subviews.first! alertContentView.backgroundColor = UIColor.darkGray alertContentView.layer.cornerRadius = 5 

Swift2及以下

  let subview :UIView = alert.view.subviews.last! as UIView let alertContentView = subview.subviews.last! as UIView alertContentView.backgroundColor = UIColor.blackColor() 

目标-C

 UIView *subView = alertController.view.subviews.lastObject; //firstObject UIView *alertContentView = subView.subviews.lastObject; //firstObject [alertContentView setBackgroundColor:[UIColor darkGrayColor]]; alertContentView.layer.cornerRadius = 5; 

由于已知错误( https://openradar.appspot.com/22209332 ),所接受的解决scheme在iOS 9上不起作用。

看到我的完整答案在这里: https : //stackoverflow.com/a/37737212/1781087

对于Objective C,下面的代码就像魅力一样。

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Save changes?" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIView *firstSubview = alertController.view.subviews.firstObject; UIView *alertContentView = firstSubview.subviews.firstObject; for (UIView *subSubView in alertContentView.subviews) { //This is main catch subSubView.backgroundColor = [UIColor blueColor]; //Here you change background }