My Connections

Business card

Monday, August 30, 2010

How to customize Alert View

Customize Alert View like...

1. Changing background color of a alert view...
2. Change in Alert View Buttons.. etc.

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"My Alert"
message: nil delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Button1", @"Button2", @"Button3", nil] autorelease];
[theAlert show];

//Set View of Button Cancel at index 1 in AlertView.
[[[theAlert subviews] objectAtIndex:1] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"btnCancelBG.png"]]];
[[[theAlert subviews] objectAtIndex:1] setShadowColor:[UIColor clearColor]];

//Set View of Button at index 2 in AlertView.
[[[theAlert subviews] objectAtIndex:2] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button1.png"]]];
[[[theAlert subviews] objectAtIndex:2] setShadowColor:[UIColor clearColor]];
[[[theAlert subviews] objectAtIndex:2] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

//Set View of Button at index 3 in AlertView.
[[[theAlert subviews] objectAtIndex:3] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button2.png"]]];
[[[theAlert subviews] objectAtIndex:3] setShadowColor:[UIColor clearColor]];
[[[theAlert subviews] objectAtIndex:3] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

//Set View of Button at index 4 in AlertView.
[[[theAlert subviews] objectAtIndex:4] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button3.png"]]];
[[[theAlert subviews] objectAtIndex:4] setShadowColor:[UIColor clearColor]];
[[[theAlert subviews] objectAtIndex:4] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
[theTitle setFont:[UIFont fontWithName:@"Helvetica" size:16]];
[theTitle setTextAlignment:UITextAlignmentLeft];

UIImage *theImage = [UIImage imageNamed:@"alertBG.png"];
CGSize theSize = [theAlert frame].size;

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

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






to get a running Sample Code Click here...

2 comments:

  1. this is not working with ios 4.2 or letter. :(

    ReplyDelete
  2. @Swami I have updated the code, U can use it now.

    ReplyDelete