How to Change the Background Color of the Uialertcontroller

change UIAlertcontroller background Color

try this

Swift2 and below

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

Objective -C

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

alertContentView.layer.cornerRadius = 5;

updated answer swift 3 and above

   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, animated: true, 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)

output

iPhone

Sample Image

iPad

Sample Image

How to set background color in UIAlertController

After my test, I figure out the way to do it with code:

okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor=UIColor.Green;

UIAlertController change background color of Cancel button for action sheet

You cannot change color of a default cancel button style. You need to create a custom view controller for the cancel button and set it as a content view controller of a cancel alert action. This way keeps the cancel button separately

Sample Image

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

alertController.addAction(UIAlertAction(title: "Option 1", style: .default, handler: nil))
alertController.addAction(UIAlertAction(title: "Option 2", style: .default, handler: nil))
alertController.addAction(UIAlertAction(title: "Option 3", style: .default, handler: nil))

alertController.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: nil))

if let firstSubview = alertController.view.subviews.first, let alertContentView = firstSubview.subviews.first {
for view in alertContentView.subviews {
view.backgroundColor = .darkGray
}
}

alertController.view.tintColor = .white

let cancelButtonViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CancelButtonViewController")
let cancelAction = UIAlertAction(title: "", style: .cancel, handler: nil)
cancelAction.setValue(cancelButtonViewController, forKey: "contentViewController")

alertController.addAction(cancelAction)

present(alertController, animated: true, completion: nil)

Sample Image

Swift UIAlertController background color

I've found a solution, I'm not too proud of it, because it's not elegant and out of the ordinary, but it works!
Apparently the number of subviews in iOS before 10 results in 1 when checked with "cont" then either .last or .fist would work, but in iOS 10 that number is different from 1, I could not determine if it would be constant, But I decided to make a "for" to make all the possibilities.

alertController.view.tintColor = UIColor.whiteColor()
for i in 0 ..< alertController.view.subviews.count {
alertController.view.subviews[i].backgroundColor = corAzul
alertController.view.subviews[i].layer.cornerRadius = 5.0
alertController.view.subviews[i].alpha = 1.0
for ii in 0 ..< alertController.view.subviews[i].subviews.count {
alertController.view.subviews[i].subviews[ii].backgroundColor = corAzul
alertController.view.subviews[i].subviews[ii].layer.cornerRadius = 5.0
alertController.view.subviews[i].subviews[ii].alpha = 1.0
for iii in 0 ..< alertController.view.subviews[i].subviews[ii].subviews.count {
alertController.view.subviews[i].subviews[ii].subviews[iii].backgroundColor = corAzul
alertController.view.subviews[i].subviews[ii].subviews[iii].layer.cornerRadius = 5.0
alertController.view.subviews[i].subviews[ii].subviews[iii].alpha = 1.0

}
}

}

I know this is not pretty, but it was the only solution that worked!
If someone finds something more sophisticated, feel free to contribute



Related Topics



Leave a reply



Submit