0

I'm trying to set the background color of a UIAlertController. The code I'm using appears to (almost) do that except it looks more like Tint than BackgroundColor. The corners are the color I'm setting it to but the main body of the alert looks more like it's tinted. My understanding is that the actual alert view is the last subview of the UIAlertController.

My code:

var okAlertController = UIAlertController.Create(message, messagetype, UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

int lastView = okAlertController.View.Subviews.Length - 1;

if (messagetype.ToUpper() == "ERROR")
{ 
    okAlertController.View.Subviews[lastView].BackgroundColor = new UIColor(CoreImage.CIColor.RedColor );
} 
else if (messagetype.ToUpper() == "SUCCESS") 
{
    okAlertController.View.Subviews[lastView].BackgroundColor = new UIColor(CoreImage.CIColor.GreenColor);
}

Is this correct? I was looking for a solid green, red or default based on the message type I was displaying, or is this the only thing I can do?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34

1 Answers1

0

After my test, I figure out the way to do it with code:

okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor=UIColor.Green;
Adrain
  • 1,946
  • 1
  • 3
  • 7
  • I see swift code all over the place that is able to change the background color. I just can't make sense of it. For example: https://stackoverflow.com/questions/37293656/change-uialertcontroller-background-color – Prescott Chartier Mar 03 '22 at 03:56
  • check the update above. – Adrain Mar 03 '22 at 05:16
  • Well that did it. I had already tried that earlier but now I'm thinking that I got wrapped around the axle with `BackgroundColor = new UIColor(CoreImage.CIColor.GreenColor)` instead of `BackgroundColor=UIColor.Green` in any event, it now does what I want. Thanks. – Prescott Chartier Mar 03 '22 at 12:02