2016-05-18 25 views
5

Ok, więc mam ten alert, którego używam i chcę, aby jego tło było czarne, a nie szare, tak jak jest. Udało mi się zmienić kolor tekstu tytułu i wiadomości, ale nie kolor tła. Dobrze do pożądanego koloru, który chcę. Zmieniłem go na zielony, niebieski i biały, ale nie czarny. Kiedy próbuję zmienić kolor na czarny, zmienia kolor na szary. Wszelkie sugestie pomogą i zostaną docenione. Próbowałem tego tutaj How to change the background color of the UIAlertController? i tak właśnie dotarłem tam, gdzie teraz jestem.zmiana koloru tła UIAlertcontroller Kolor

Oto co mam zamiar teraz:

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, tak naprawdę mam do tego miejsca. Jeśli spojrzysz na mój kod, to jest dokładnie taki sam, jak jedna z sugestii. – MNM

Odpowiedz

9

spróbować tej

Swift3

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

Swift2 i poniżej

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 tak, to trochę ostatnie! był świetny, dziękuję bardzo – MNM

+1

@MNM - witamy bro –

+1

@ Anbu.Karthik kod celu, który podałeś nie działa dla mnie. 'UIView * subView = alertController.view.subviews.lastObject; UIView * alertContentView = subView.subviews.lastObject; [alertContentView setBackgroundColor: [UIColor darkGrayColor]]; alertContentView.layer.cornerRadius = 10; ' Stil kolor tła pozostaje biały. – Aneesh

3

W przypadku Celu C poniższy kod działa jak urok.

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 
}