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
iPad
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
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)
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
App Installation Failed Due to Application-Identifier Entitlement
Xcode iOS Project Only Shows "My MAC 64-Bit" But Not Simulator or Device
Programmatically Scroll a Uiscrollview
Libraries Not Found When Using Cocoapods with iOS Logic Tests
Save and Load from Keychain | Swift
Uitableview Dynamic Cell Heights Only Correct After Some Scrolling
How to Make View the Size of Another View in Swiftui
How to Attach Debugger to iOS App After Launch
Check If Uicolor Is Dark or Bright
Usage of Protocols as Array Types and Function Parameters in Swift
How to Disable/Enable the Sleep Mode Programmatically in iOS
Add a Uiview Above All, Even the Navigation Bar
Changing Placeholder Text Color with Swift