好的,所以我有这个警告,我正在使用它,我希望它的背景是黑色而不是灰色。我已设法更改标题和消息的文本颜色,但不更改背景颜色。那么我想要的颜色。我已将其更改为绿色蓝色和白色,但不是黑色。当我尝试将其更改为黑色时,它会变为灰色。任何建议都会有所帮助并受到赞赏。我在这里尝试了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, 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
}
答案 0 :(得分:14)
试试这个
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;
更新了答案swift 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, 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)
<强>输出强>
<强> iPhone 强>
<强> ipad公司强>
答案 1 :(得分:14)
Swift 4.1:
这是最适合我的方法:
func testAlert(){
let alert = UIAlertController(title: "Let's See ..",message: "It Works!", preferredStyle: .alert)
let dismissAction = UIAlertAction(title: "Dismiss", style: .default, handler: nil)
// Accessing alert view backgroundColor :
alert.view.subviews.first?.subviews.first?.subviews.first?.backgroundColor = UIColor.green
// Accessing buttons tintcolor :
alert.view.tintColor = UIColor.white
alert.addAction(dismissAction)
present(alert, animated: true, completion: nil)
}
答案 2 :(得分:5)
对于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
}
答案 3 :(得分:3)
由于已知错误(https://openradar.appspot.com/22209332),接受的解决方案无法在iOS 9上运行。
请在此处查看完整答案:https://stackoverflow.com/a/37737212/1781087
答案 4 :(得分:0)
如果某人想要具有不透明的白色背景色,则可以使用这种衬纸来做到这一点:
UIVisualEffectView.appearance(whenContainedInInstancesOf: [UIAlertController.classForCoder() as! UIAppearanceContainer.Type]).backgroundColor = UIColor.white
但是请注意,这仅适用于白色,因为默认的视觉效果,其他颜色会有所不同。
答案 5 :(得分:0)
让subview =(alert.view.subviews.first?.subviews.first?.subviews.first!)!作为UIView subview.layer.cornerRadius = 1 subview.backgroundColor = UIColor.white
答案 6 :(得分:-1)
雨燕5
使用UIAlertController扩展只编写一行代码。
alertController.setBackgroundColor(color: UIColor.black)
完整文档:http://www.swiftdevcenter.com/change-font-text-color-and-background-color-of-uialertcontroller/
extension UIAlertController {
//Set background color of UIAlertController
func setBackgroundColor(color: UIColor) {
if let bgView = self.view.subviews.first, let groupView = bgView.subviews.first, let contentView = groupView.subviews.first {
contentView.backgroundColor = color
}
}
//Set title font and title color
func setTitlet(font: UIFont?, color: UIColor?) {
guard let title = self.title else { return }
let attributeString = NSMutableAttributedString(string: title)//1
if let titleFont = font {
attributeString.addAttributes([NSAttributedString.Key.font : titleFont],//2
range: NSMakeRange(0, title.utf8.count))
}
if let titleColor = color {
attributeString.addAttributes([NSAttributedString.Key.foregroundColor : titleColor],//3
range: NSMakeRange(0, title.utf8.count))
}
self.setValue(attributeString, forKey: "attributedTitle")//4
}
//Set message font and message color
func setMessage(font: UIFont?, color: UIColor?) {
guard let message = self.message else { return }
let attributeString = NSMutableAttributedString(string: message)
if let messageFont = font {
attributeString.addAttributes([NSAttributedString.Key.font : messageFont],
range: NSMakeRange(0, message.utf8.count))
}
if let messageColorColor = color {
attributeString.addAttributes([NSAttributedString.Key.foregroundColor : messageColorColor],
range: NSMakeRange(0, message.utf8.count))
}
self.setValue(attributeString, forKey: "attributedMessage")
}
//Set tint color of UIAlertController
func setTint(color: UIColor) {
self.view.tintColor = color
}
}