views:

6610

answers:

9

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

A: 

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
+1  A: 

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
Not allowing to change the colour will make app developers create their own UI elements which could end up being even more confusing ;)
Thomas
A: 

See this question.

Mr. Matt
-1: Exact duplicates should be noted in a comment, rather than an answer
Casebash
+7  A: 

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
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
how can i make the image to scale until it fits the alertview??
aryaxt
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
A: 

To add to oxigen's post (how do I add a comment to a post, can't seem to find a button to do that), if you don't want a warning, change the last line to:

alert.layer.contents = (id)[theImage CGImage];

Aral Balkan
you need a rep of 50 to be allowed to leave comments...
squelart
Ah, k, thanks. Possibly one bit of the otherwise excellent badge/accomplishments system here that I'm not a fan of then :)
Aral Balkan
-1: Should be a comment, not an answer. I recommend deleting this answer and rewriting it as a comment.
Casebash
And so it was that the pretty-famous Aral Balkan left StackOverflow and never came back...
Yar
+2  A: 

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 }

marcio
A: 

The last line of the code:

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

Creates the following warning in xcode:

warning: no '-setContents:' method found

Is this something I should just ignore because this isn't really a "supported" method? Or is it something that has changed in recent SDK?

hookjd
-1: Should be a comment, not an answer. Commenting requires 50 reputation, review the FAQ about the reputation system.
Casebash
A: 

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

Vijay Shankar
-1: Should be a comment, not an answer. Commenting requires 50 reputation, review the FAQ about the reputation system.
Casebash
thanks adding QuartzCore fixed the problem
aryaxt
+5  A: 

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