Change UIAlertcontroller Interface Color

Ok, so I have this warning, which I use, and I want its background to be black, not gray. I managed to change the text color for the title and message, but not the background color. Well to the desired color I want. I changed it to green and white, but not black. When I try to change it to black, it turns gray. Any suggestions would help and would be appreciated. I tried it here. How to change the background color of the UIAlertController? and that's how I got to where I am now.

Here is what I am doing now:

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 } 
+5
source share
3 answers

try it

Swift3

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

Swift2 and below

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

Target -C

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

Due to a known error ( https://openradar.appspot.com/22209332 ), the decision made does not work on iOS 9.

See my full answer here: fooobar.com/questions/496916 / ...

+3
source

For Objective-C, the code below works like a charm.

 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 } 
+3
source

All Articles