Changing the background color of a UIAlertView?

12 Solutions Collect From Internet About “Changing the background color of a UIAlertView?”

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;

   [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
   theImage = UIGraphicsGetImageFromCurrentImageContext();    

   [[theAlert layer] setContents:[theImage CGImage]];

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

I recently needed this and ended up subclassing UIAlertView. Here is the code for anyone who is interested.

Well i solved the problem in a different way. I searched for the UIImageView which contains the background image and change the image.

… well show is maybe not the best solution for altering the view …

@implementation CustomAlertView

- (void)show {
    [super show];

    for (UIView *view in self.subviews) {
        NSLog(@"%@ with %i children", view, [view.subviews count]);
        // change the background image
        if ([view isKindOfClass:[UIImageView class]]) {
            UIImage *theImage = [UIImage imageNamed:@"password entry bg - default.png"];    
            ((UIImageView *)view).image = [theImage resizableImageWithCapInsets:UIEdgeInsetsMake(49, 8, 8, 8)];


Here’s a much more recent solution that takes advantage of blocks and is modeled after TweetBot:

This is not something that is possible.

Either you need to create your own alert-type view (including providing the animation, etc.) or use the default UIAlertView supplied by Apple.

Apple tends to not expose things like the color of the UIAlertView so that the UI has a common feel across all applictions. Changing the color from app to app would be confusing to the user.

Add QuartzCore framework to the application and include #import “QuartzCore/CALayer.h” in the source file.

I recently found the GRAlertView, which allow you to tailor UIAlertView in an easy way that at least I like. You find it here:

You have to use this:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are Select Row of" message:@"Human" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        alert.backgroundColor=[UIColor redColor];
        [alert show];
        [alert release];

it will changed the background color of UIAlertView.

Simply use this code,

 UIImage *theImage = [UIImage imageNamed:@"imageName.png"];
 UIView *view=[alert valueForKey:@"_backgroundImageView"];
 //Set frame according to adustable
  UIImageView *image=[[UIImageView alloc] initWithImage:theImage];
 [image setFrame:CGRectMake(0, 0, 280, 130)];
 [view addSubview:image];

I don’t believe that there is an exposed method or property for doing this. Setting the backgroundColor of the UIAlertView (as a UIView) merely puts a colored rectangular backdrop behind the alert view.

I’d say that it would probably go against the common interface of the iPhone to alter this color, so I don’t think it’s recommended behavior (in the same way that changing the background color of an alert view in Mac OS X is not recommended).