I want to create a custom alert view
within my iOS application. For example, I want to put some images
in this alert
, and change its
Create one sub class for UIAlertView.
And create common class for Alert View method. Add 2 below method for it.
#pragma mark Alert View Functions
+(void)alertViewWithYesNo:(NSString *)pstrTitle:(NSString *)pstrMessage:(int)pstrTagId:(id)pDelegate{
UIAlertView *objAlertNotify = [[UIAlertView alloc] init];
[objAlertNotify setDelegate:pDelegate];
[objAlertNotify addButtonWithTitle:@""];
[objAlertNotify addButtonWithTitle:@""];
int intTemp = 1;
for (UIView* view in [objAlertNotify subviews])
{
if ([[[view class] description] isEqualToString:@"UIAlertButton"])
{
UILabel *theTitle = [[UILabel alloc] init];
[theTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:g_AlertFontSize]];
[theTitle setTextColor:[UIColor whiteColor]];
switch (intTemp) {
case 1:
[theTitle setText:@"Yes"];
//[theTitle setTextColor:g_ColorYes];
break;
case 2:
[theTitle setText:@"No"];
//[theTitle setTextColor:g_ColorNo];
break;
}
intTemp++;
[theTitle setBackgroundColor:[UIColor clearColor]];
[theTitle setTextAlignment:UITextAlignmentCenter];
[view addSubview:theTitle];
}
else if ([[[view class] description] isEqualToString:@"UIThreePartButton"])
{
UILabel *theTitle = [[UILabel alloc] init];
[theTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:g_AlertFontSize]];
[theTitle setTextColor:[UIColor whiteColor]];
switch (intTemp) {
case 1:
[theTitle setText:@"Yes"];
//[theTitle setTextColor:g_ColorYes];
break;
case 2:
[theTitle setText:@"No"];
//[theTitle setTextColor:g_ColorNo];
break;
}
intTemp++;
[theTitle setBackgroundColor:[UIColor clearColor]];
[theTitle setTextAlignment:UITextAlignmentCenter];
[view addSubview:theTitle];
}
}
[objAlertNotify setTag:pstrTagId];
[objAlertNotify setTitle:pstrTitle];
[objAlertNotify setMessage:pstrMessage];
[objAlertNotify show];
}
+(void)alertViewBtnText:(UIAlertView *)alertView{
for (UIView* view in [alertView subviews])
{
//NSLog(@"%@", [[view class] description]);
if ([[[view class] description] isEqualToString:@"UIAlertButton"])
{
for (UILabel *lbl in [view subviews])
{
//NSLog(@"%@", [[lbl class] description]);
if ([[[lbl class] description] isEqualToString:@"UILabel"])
{
CGRect frame = [view bounds];
CGSize maximumLabelSize = CGSizeMake(320,480);
CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode];
CGRect newFrame = lbl.frame;
newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
lbl.frame = newFrame;
//frame.size.width = 320.0;
//frame.size.height = 480.0;
lbl.frame = frame;
[lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];
}
}
}
else if ([[[view class] description] isEqualToString:@"UIThreePartButton"])
{
for (UILabel *lbl in [view subviews])
{
CGRect frame = [view bounds];
CGSize maximumLabelSize = CGSizeMake(320,480);
CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode];
CGRect newFrame = lbl.frame;
newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
lbl.frame = newFrame;
//frame.size.width = 320.0;
//frame.size.height = 480.0;
lbl.frame = frame;
[lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];
}
}
}
}
Now, in whatever class, you are using this custom Alert: Add below:
#pragma mark UIAlertViewDelegate
-(void)willPresentAlertView:(UIAlertView *)alertView{
if(alertView==objAlertMsg){
/*clsCommonFuncDBAdapter *objclsCommonFuncDBAdapter = [[clsCommonFuncDBAdapter alloc] init];
float newHeight = [objclsCommonFuncDBAdapter getAlertHeightByMessage:alertView.frame.size.width :alertView.message] + [g_AlertExtraHeight intValue];
[objclsCommonFuncDBAdapter release];
//NSLog(@"X = %f, Y = %f, Widht = %f, Height = %f", alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, alertView.frame.size.height);
//[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, 110.0)];
[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, newHeight)];*/
}
[clsCommonFuncDBAdapter alertViewBtnText:alertView];
}
For calling it: Use like below:
-(void)askForGPSEnable{
[clsCommonFuncDBAdapter alertViewWithYesNo:msgGPSTitle :msgGPSMessage :0 :self];
}
Let me know in case of any difficulty.
You'll need to create your own custom view, and implement some alert view style behaviour, such as displaying modally, dimming the background, animating in and out, etc.
There's no support in the SDK for customising a UIAlertView any further than the text or buttons.
http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
and
http://www.skylarcantu.com/blog/2009/08/14/custom-uialertview-color-chooser/
You can use above links for custom alerts. I hope these will be helpful for you.
There is very good example on Git hub for custom alert view. It is using UI alert view in core and provides number of methods to customize alert view in different ways
Here's a custom alert view that I wrote which is a drop-in replacement for UIAlertView. You can set a custom background image; it wouldn't be hard to extend to support custom background colors.
https://github.com/TomSwift/TSAlertView
I set up my own UIViewController which I can skin with my own images. I generally only use one or two buttons, so I hide the second button if it's not being used. The view is actually the size of the entire screen, so it blocks touches behind it, but it is mostly transparent, so the background shows through.
When bringing it in, I use a few animations to make it bounce like Apple's alert view. Something like this works:
-(void)initialDelayEnded {
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
self.view.alpha = 1.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)];
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounce1AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)];
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounce2AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
I have the possibility of a short delay built into the class, so initialDelayEnded is called when that delay is over.
When initializing, I pass in an object and selector I want called when each button is pressed, and then I call the appropriate selector on the object when a button is pressed.