2016-05-18 18 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

@ लुकाडावनजो वास्तव में मुझे इस स्थान पर कैसे पहुंचा। यदि आप मेरे कोड को देखते हैं तो यह – 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() 

उद्देश्य -सी

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 उद्देश्य सी कोड जिसे आपने मेरे लिए काम नहीं किया है। 'UIView * subView = alertController.view.subviews.lastObject; UIView * alertContentView = subView.subviews.lastObject; [चेतावनी सामग्री देखें सेटबैकग्राउंड रंग: [यूआईसीओलर darkGraycolor]]; alertContentView.layer.cornerRadius = 10; ' पृष्ठभूमि रंग को सफ़ेद के रूप में स्थिर रखें। – Aneesh

3

उद्देश्य सी के लिए, नीचे कोड आकर्षण की तरह काम करता है।

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 
} 
संबंधित मुद्दे