Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the background color of a UIAlertView?

I want to change the background color of my UIAlertView, but this doesn't appear to have a color attribute.

like image 502
Aishwarya Avatar asked May 19 '09 14:05

Aishwarya


2 Answers

Background of AlertView is an image And you can change this image

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention"    message: @"YOUR MESSAGE HERE", nil)    delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];     [theAlert show];     UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];    [theTitle setTextColor:[UIColor redColor]];     UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];    [theBody setTextColor:[UIColor blueColor]];     UIImage *theImage = [UIImage imageNamed:@"Background.png"];        theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16];    CGSize theSize = [theAlert frame].size;     UIGraphicsBeginImageContext(theSize);        [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];        theImage = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();     [[theAlert layer] setContents:[theImage CGImage]]; 
like image 111
oxigen Avatar answered Sep 23 '22 12:09

oxigen


I've also find an almost identical workaround and, in my case, it works better. Using oxigen way out i've discovered a poor result on the screen and the context get blurred. Rather than subclassing UIAlertView I implement:

- (void)willPresentAlertView:(UIAlertView *)alertView; 

so...just copy & paste

- (void)willPresentAlertView:(UIAlertView *)alertView  {     blah blah blah...     [[alertView layer] setContents:(id)theImage.CGImage];  // to avoid the warning } 
like image 31
marcio Avatar answered Sep 19 '22 12:09

marcio