38

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

Casebash
  • 114,675
  • 90
  • 247
  • 350
Aishwarya
  • 1,587
  • 3
  • 14
  • 9

12 Answers12

34

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]];
oxigen
  • 6,263
  • 3
  • 28
  • 37
  • Make sure to look at: http://stackoverflow.com/questions/883208/how-to-change-background-color-of-uialertview/1478873#1478873 for some suggestions for improvement – Casebash Feb 18 '10 at 02:58
  • how can i make the image to scale until it fits the alertview?? – aryaxt Aug 05 '10 at 00:35
  • I tried this but it didn't work. Is there a requirement for the image size as right now I am using a hi-resolution image. – Ankur Oct 07 '10 at 22:00
  • 3
    This doesnt seem to work on iOS 4.2 anymore... i've been using this on earlier iOS... im still finding another way. – Underdog Mar 01 '11 at 11:04
  • 1
    it's still showing the default alert background on top of my custom image...any idea? – raistlin Feb 26 '13 at 12:17
13

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
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
marcio
  • 2,729
  • 2
  • 25
  • 23
10

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

http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color

kwigbo
  • 117
  • 1
  • 3
  • 1
    This solution is nice in that there in that there is no need to produce a background image, however it doesn't quite look as good as the version that Apple made. – Casebash Feb 18 '10 at 02:55
7

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)];
        }
    }
}

@end
informatics
  • 103
  • 1
  • 5
  • 1
    This approach still works as of iOS 5, and the replacement of the image can be done in the '- (void)willPresentAlertView:(UIAlertView *)alertView' method, which doesn't require overriding the 'show' method. – software evolved Feb 23 '12 at 03:21
  • Just thought I would clarify: if you are creating a subclass of UIAlertView, override -(void)show. OR you can include the code above inside of willPresentAlertView in the delegate. – software evolved Mar 02 '12 at 04:27
4

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

https://github.com/Arrived/BlockAlertsAnd-ActionSheets

esilver
  • 27,713
  • 23
  • 122
  • 168
  • 1
    So it is. Here's a fork of that branch: https://github.com/gpambrozio/BlockAlertsAnd-ActionSheets – esilver Mar 27 '12 at 17:59
  • Warning about that class - it does not know how to display itself properly when the device is rotated. It also is not compatible with iOS 3.1.x – esilver May 25 '12 at 00:47
3

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.

August
  • 12,139
  • 3
  • 29
  • 30
  • 16
    Not allowing to change the colour will make app developers create their own UI elements which could end up being even more confusing ;) – Thomas Sep 05 '10 at 01:07
2

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

Vijay Shankar
  • 178
  • 2
  • 3
1

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.

USK
  • 499
  • 1
  • 4
  • 9
  • This changes the backgroundcolor from clearColor to a solid color resulting in a very poor looking UIAlertView. – TMilligan May 14 '13 at 02:18
1

This looks like the most robust solution to me

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

Luke
  • 3,375
  • 2
  • 22
  • 22
1

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

PeterK
  • 4,243
  • 4
  • 44
  • 74
0

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];
Rajesh Maurya
  • 3,026
  • 4
  • 19
  • 37
-3

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).

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • 5
    True but I suppose if Apple imposes rules on us they should follow similar rules too, am I right? If you see GameKit's AlertViews all of them are with black background colour. Strange! – TeaCupApp Jan 20 '12 at 00:22