2016-05-18 319 views
5

好吧,所以我有这个警报,我正在使用,我想它的背景是黑色的,而不是灰色的。我设法改变标题和信息的文字颜色,但不改变背景颜色。以及我想要的颜色。我已将其更改为绿色蓝色和白色,但不是黑色。当我尝试将它改为黑色时,它会变成灰色。任何建议都会有所帮助,值得赞赏。我在这里尝试了这个How to change the background color of the UIAlertController?,这就是我现在所处的位置。更改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 



} 
+1

@LucaDavanzo这实际上是我如何到达这个位置。如果你看我的代码,它是完全相同的建议 – MNM

回答

9

试试这个

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; 
+0

哎呀是最后一点!是诀窍非常感谢你 – MNM

+1

@MNM - 欢迎兄弟 –

+1

@ Anbu.Karthik您提供的客观c代码不适合我。 'UIView * subView = alertController.view.subviews.lastObject; UIView * alertContentView = subView.subviews.lastObject; [alertContentView setBackgroundColor:[UIColor darkGrayColor]]; alertContentView.layer.cornerRadius = 10;' Stil背景颜色保持为白色。 – Aneesh

3

对于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 
}