2016-05-18 12 views
5

Ok, also ich habe diese Warnung, die ich verwende, und ich möchte den Hintergrund davon schwarz sein, nicht grau wie es ist. Ich habe es geschafft, die Farbe des Textes für den Titel und die Nachricht, aber nicht die Hintergrundfarbe zu ändern. Gut zur gewünschten Farbe, die ich will. Ich habe es in grün blau und weiß geändert, aber nicht schwarz. Wenn ich versuche, es in Schwarz zu ändern, wird es grau. Irgendwelche Vorschläge helfen und werden geschätzt. Ich habe das hier probiert How to change the background color of the UIAlertController? und so kam ich dahin, wo ich jetzt bin. Hierändern UIAlertcontroller Hintergrundfarbe

ist, was ich jetzt gehen:

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, das ist eigentlich, wie ich an diesen Ort kam. Wenn du dir meinen Code anschaust, ist das genau der selbe wie einer der Vorschläge – MNM

Antwort

9

versuchen, diese

swift3

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

Swift2 und unter

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

Objective C

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

alertContentView.layer.cornerRadius = 5; 
+0

Heck ja das letzte bisschen! war der Trick vielen Dank – MNM

+1

@MNM - willkommen bro –

+1

@ Anbu.Karthik der objektive c-Code, den Sie zur Verfügung gestellt haben funktioniert nicht für mich. 'UIView * Unteransicht = alertController.viewnoviews.lastObject; UIView * alertContentView = subView.views.lastObject; [alertContentView setBackgroundColor: [UIColor darkGrayColor]]; alertContentView.layer.cornerRadius = 10; ' Stil bleibt die Hintergrundfarbe als weiß. – Aneesh

3

Für Ziel C funktioniert der folgende Code wie Charme.

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 
}