我想在iOS应用程序中创建自定义alert view
。例如,我想在images
中添加一些alert
,并更改其颜色。
我知道如何创建普通UIAlertView
,但有没有办法自定义alert view
?
答案 0 :(得分:39)
我设置了自己的UIViewController,我可以使用自己的图像进行修饰。我通常只使用一个或两个按钮,所以如果它没有被使用我隐藏第二个按钮。视图实际上是整个屏幕的大小,因此它会阻挡它后面的触摸,但它大部分是透明的,所以背景显示出来。
当它带入时,我会使用一些动画让它像Apple的警报视图一样反弹。这样的事情有效:
-(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];
}
我有可能在类中内置一个短暂的延迟,因此当延迟结束时会调用initialDelayEnded。
初始化时,我会在按下每个按钮时传入一个我想要调用的对象和选择器,然后在按下按钮时调用对象上的相应选择器。
答案 1 :(得分:12)
这是我写的自定义警报视图,它是UIAlertView的替代品。您可以设置自定义背景图像;扩展以支持自定义背景颜色并不难。
答案 2 :(得分:4)
为UIAlertView创建一个子类。
为Alert View方法创建公共类。 为它添加以下方法。
#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)];
}
}
}
}
现在,在任何课程中,您都在使用此自定义警报: 添加如下:
#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];
}
用于调用它: 使用如下:
-(void)askForGPSEnable{
[clsCommonFuncDBAdapter alertViewWithYesNo:msgGPSTitle :msgGPSMessage :0 :self];
}
如有任何困难,请告诉我。
答案 3 :(得分:2)
您需要创建自己的自定义视图,并实现一些警报视图样式行为,例如以模态显示,调暗背景,动画进出等等。
SDK中没有支持自定义UIAlertView的任何文本或按钮。
答案 4 :(得分:2)
http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
和
http://www.skylarcantu.com/blog/2009/08/14/custom-uialertview-color-chooser/
您可以使用以上链接进行自定义提醒。我希望这些对你有所帮助。
答案 5 :(得分:1)
custom alert view的Git中心有很好的例子。它在核心中使用UI警报视图,并提供了多种以不同方式自定义警报视图的方法