Search code examples
c#xamarin.iosuialertcontrollervisual-studio-2022

How to set background color in UIAlertController


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?


Solution

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

    okAlertController.View.Subviews[0].Subviews[0].Subviews[0].BackgroundColor=UIColor.Green;